Looking to display a value from the 1st record's 'post' field which is stored in a MySQL database using PHP. The following div is where the value is to be inserted:
<div id="insert1"><?php echo($r); ?></div>
The PHP (Note using MeekroDB):
<?php
require_once 'meekrodb.2.2.class.php';
DB::$user = 'username';
DB::$password = 'password';
DB::$dbName = 'database';
DB::$host = 'hostname';
// get all entries from database table Microblog;
$results = DB::query("SELECT post FROM MicroBlog");
foreach ($results as $row) {
$id = $row['id'];
// loop through each entry - there's 3
for($i = 0; $i < 3; $i++) {
// if it's the 1st entry (id = 1) then get field 'post'
if ($id == 1) {
$r = $row['post'];
}
}
}
?>
Currently #insert1 is blank. What do I need to change to get it into the div? Please note, I want to keep the for loop, as I'll be adding other if's once I get it running. Thanks.
Probably this part is wrong:
// get all entries from database table Microblog;
$results = DB::query("SELECT post FROM MicroBlog");
should be:
// get all entries from database table Microblog;
$results = DB::query("SELECT * FROM MicroBlog");
You're getting id which i think you're not getting from your query.
$id = $row['id'];
Other option:
Comment out $id.
//$id = $row['id'];
and in the if block below, change $id to $i:
if ($i == 1) {
$r = $row['post'];
}
To add number of records:
$x = 0;
foreach ($results as $row) {
$x++;
$id = $row['id'];
// loop through each entry - there's 3
for($i = 0; $i < 3; $i++) {
// if it's the 1st entry (id = 1) then get field 'post'
if ($id == 1) {
$r = $row['post'];
}
}
}
echo $x;
Related
<?php
include ("../pos/db_config/db_config.php");
include ("../pos/functions/select.php");
$i = 0;
$sql = topsalep($idCompany);
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$end_sale_date = $row["end_sale_date"];
$name = $row["name"];
$tot_s = $row["tot_price"];
$qc = $row["sales_item_qty"];
$proResult[$i][0] = $name;
$proResult[$i][1] = $end_sale_date;
$proResult[$i][2] = $tot_s;
$proResult[$i][3] = $qc;
$i++;
}
//$name = $proResult[0][0];
//echo "$name";
for ($i = 0; $i < $result->num_rows; $i++) {
$count = 0;
$name = $proResult[$i][0];
$date = $proResult[$i][1];
$price = $proResult[$i][2];
$qct = $proResult[$i][3];
$totalPrice = 0;
for ($j = 0; $j < $result->num_rows; $j++) {
if ($proResult[$j][1] == $date && $proResult[$j][0] == $name) {
$x = $proResult[$j][2];
$count++;
$totalPrice+=$x;
}
}
$name = $proResult[$i][0];
$date = $proResult[$i][1];
$price = $proResult[$i][2];
for ($k = 0; $k < $count; $k++){
if($proResult[$k][1] == $date && $proResult[$k][0] == $name)
{
}
}
echo "Name : $name <br>";
echo "End Date : $date : Count $count : total = $totalPrice <br><br>";
?>
In this code I showed Last 5 days product vise total value. A one day can happen same sale id there different quantity of products. Then finally I calculated the one product wise total sale value. But problem is duplicate it.
In my join query I used distinct But I want show my PHP code when duplicated values only show one value.
enter image description here
enter image description here
I am using the Eloquent library and want to pull out every row of my table.
I have the following code:
<?php
$skole = null;
if(isset($_GET['id'])) {
include 'connection.php';
$skole = Skole::find($_GET['id']);
}
if($skole != null) {
$identifikasjon = $_GET['id'];
$skoleTilArrangement = Skole::where('id', $identifikasjon)->with('arrangementer')->get();
for($i = 0, $l = count($skoleTilArrangement); $i < $l; ++$i) {
echo $skoleTilArrangement[$i]->arrangementer->type;
}
}
I have also tried this, which gives the same result (I can only pull out 1 out of 4 rows):
if($skole != null) {
$identifikasjon = $_GET['id'];
$skoleTilArrangement = Skole::where('id', $identifikasjon)-
>with('arrangementer')->get();
echo $skoleTilArrangement->arrangementer->type;
}
However, with this i can pull out the first row only:
echo $skoleTilArrangement[0]->arrangementer->type;
Any suggestions?
Im creating tablerows based on the number of the array colours:
$query = mysql_query("SELECT * FROM things);
$num = mysql_num_rows($query );
$colours = array ();
if($num)
{
for ($i = 0; ($row = mysql_fetch_assoc($query)); ++$i)
{
$colours[$i] = $row["colours"];
}
}
$arrlength = count($colours);
for ($i = 0; ($i < ($arrlength)); ++$i){
echo "
<tr class='border_bottom'><td>".$colours[$i]."</td></tr>
";
}
So, if colours is, lets say, equal to 8, 8 table rows with the class border_bottom are created.
border_bottom is used by CSS to add a border to the bottom of each tablerow.
What I need is some PHP help: I need code which checks the array colours. The last element of the array has to go with an empty id since I dont want a border-bottom added to that very last tablerow. All other tablerows have to go with the border_bottom class, tho.
I was thinking of starting the code like that:
echo"
<tr class='
";
-->PHP code goes here<--
echo"
'>
<td>".$colours[$i]."</td></tr>
Try this:
<?php
$query = mysql_query("SELECT * FROM things");
$num = mysql_num_rows($query);
$colours = array();
if($num)
{
while($row = mysql_fetch_assoc($query))
{
$colours[] = $row["colours"];
}
}
$arrlength = count($colours);
for ($i = 0; $i < $arrlength; ++$i){
if($i < $arrlength - 1){
echo "<tr class='border_bottom'><td>{$colours[$i]}</td></tr>";
}else{
echo "<tr><td>{$someColor}</td></tr>";
}
}
Try the following code in your table row echo
echo "<tr"
.($i < $arrlength - 1 ? " class='border_bottom'" : "")
.">"
."<td>{$colours[$i]}</td></tr>";
You can actually do this while fetching the rows without needing to count how many there are, by reading ahead one row.
$previous_row = mysql_fetch_array(); // fetch the first row (if there is one)
while ($previous_row) {
if ($row = mysql_fetch_array()) {
// if another row is fetched, then the previous row was NOT the last row
echo '<tr class="border_bottom"><td>' . $previous_row['colours'] . '</td></tr>';
} else {
// otherwise, the previous row WAS the last row, and does not get the class
echo '<tr><td>' . $previous_row['colours'] . '</td></tr>';
}
$previous_row = $row; // Set the previous row to the current row
}
I'm new at using pChart. I take data from database to construct the graph.
I will have a random number of rows and I would like to do a graph for each row.
(one row -> one graph). Is it possible?
So far I can do the graph but all the rows are in the same graph.
Here is my code:
<?php
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pImage.class.php");
include("pChart/class/pPie.class.php");
$myData = new pData();
$Requete = "SELECT * FROM `day`";
$Result = mysql_query($Requete, $db);
while($row = mysql_fetch_array($Result)){
$hour = explode(" ", $row["g1"]);
$nb = $row["numb"] * 2;
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
$time[$i] = ($hour[$i])/ 60;
$myData->addPoints($time[$i],"year");
}else{
if ($hour[$i] == "00"){
$name[$i] = "On";
}elseif ($hour[$i] == "02"){
$name[$i] = "Off";
}
$myData->addPoints($name[$i],"name");
}
}
}
$myData->setAbscissa("name");
$myData->setSerieDescription("year","Application A");
$myPicture = new pImage(600,300,$myData);
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/tahoma.ttf","FontSize"=>16));
$PieChart = new pPie($myPicture,$myData);
$PieChart->draw3DPie(340,125,array("DrawLabels"=>TRUE,"Border"=>TRUE));
$myPicture->autoOutput("images/example.png");
?>
If I understand the question, you want a picture for every row? In that case I think that you will have to include the lower part of your code (starting from $myData to $myPicture) in the while loop.
I am trying to use a for loop to select an incremental value with a MySQL query. I have included sample code below:
<?php
$day_1="sep_28";
$day_2="sep_29";
$day_3="sep_30";
$query = mysql_query("SELECT * FROM table WHERE id = '$id'");
while ($row = mysql_fetch_assoc($query))
{
for ($i = 1; $i <= 3; $i++)
{
$dayVar = "day_".$i;
//$dayVarCount = $dayVar."_count"; // Don't really need this anymore, so removed.
$dayVarCount = $row[$$dayVar];
echo "$$dayVar.': '.$dayVarCount<p>"; // Edited.
}
}
?>
I think I am getting close, but when I run the code my page is showing this:
$day_1.': '.0
$day_2.': '.2
$day_3.': '.5
Any additional recommendations? Thanks for the great help!
Try a variable variable:
for ($i = 1; $i <= 3; $i++)
{
$dayVar = "day_".$i;
$dayVarCount = $dayVar."_count";
$$dayVarCount = $row[$$dayVar];
echo $$dayVar.': '.$$dayVarCount.'<p>'; // Edited.
}
This basically uses a string to reference a variable by its name.
Just think of it this way:
$variable = 'hello';
$string = 'variable';
echo $$string;
// Is the same thing as:
echo $variable;
// Because you can thing of $$string as ${$string} ---> $variable when {$string} is interpreted into 'variable'
http://php.net/manual/en/language.variables.variable.php
Replace this line:
echo "$$dayVar.': '.$dayVarCount<p>";
with this:
echo $$dayVar . ': ' . $dayVarCount . '<br>';
<?php
$day_1="January 1";
$day_2="January 2";
$day_3="January 3";
$query = mysql_query("SELECT * FROM dates WHERE id = '$id'");
while ($row = mysql_fetch_assoc($query))
{
for ($i = 1; $i <= 3; $i++)
{
$var = $."day_".$i;
$day_$i_count=$row['$var'];
echo "$day_$i: $day_$i_count<p>";
}
}
?>
You can try this code too.
That all seems overly complicated, but possibly I don't understand the question adequately. Would this work?
$day[1]="sep_28";
$day[2]="sep_29";
$day[3]="sep_30";
$query = mysql_query("SELECT * FROM table WHERE id = '$id'");
while ($row = mysql_fetch_assoc($query))
{
foreach ($day as $day_str)
{
echo $day_str . ':' . $row[$day_str] . '<p>';
}
}