Jump to content

trying to print $result in rows of a table


farahZ

Recommended Posts

hello, i am trying to print the query $result, each in a row!!

but i am just getting the form of the table without values added to it..

the query works fine since it was outputting normally before adding the table code

any help !!

 


$result = mysqli_query($con, $sql) or die(mysqli_error());
$num_rows = mysqli_num_rows($result);
while ($row =  mysqli_fetch_array($result) && $i<$num_rows) {
?>
<tr>
<td><?php echo $row['DayTime'];?></td>
<td><?php echo $row['Calories'];?></td>
</tr>  
</table>
<?php
$i++;
}

You're ending the table with </table> inside of the while-loop but you don't start any <table> in the loop. Might that be the problem?

Move </table> outside of the loop and make sure there's a <table> tag before the loop aswell.

 

Also, you can remove this condition:

&& $i<$num_rows

The loop will stop when it's gone through the results from the query.

 

- W

i tried that, its now creating rows but not filling them!!
 thats the code i reached

 

 

 
<?php
$num_rows ='';
$i=0;
$date =$_POST['dates'];
$sql="SELECT fe.DayTime, SUM(f.Calories) as Calories 
FROM fooddiary fe
INNER JOIN food f USING (Food)
WHERE fe.ID= 111 And fe.Date='$date'
GROUP BY fe.DayTime";
$result = mysqli_query($con, $sql) or die(mysqli_error());
//var_dump($sql);
$num_rows = mysqli_num_rows($result);
?>
<table width="800" border="3">
<tr>
<th bgcolor="#77eb8a" height="25" scope="col">Meal</th>
<th bgcolor="#77eb8a" scope="col">Calories</th>
</tr>
<?php
while ($row =  mysqli_fetch_array($result) && $i<$num_rows) {
?>
<tr>
<td><?php echo $row['DayTime'];?></td>
<td><?php echo $row['Calories'];?></td>
</tr>  
 
<?php
$i++;
}
?></table>
<?php
$sql1="SELECT SUM(f.Calories) as Calories 
FROM fooddiary fe
INNER JOIN food f USING (Food)
WHERE fe.ID= 111 And fe.Date='$date'";
$result1 = mysqli_query($con, $sql1) or die(mysqli_error());
$row = mysqli_fetch_array($result1);
echo 'The total Calories for this day is ' . $row['Calories'];
mysqli_close($con);
}
 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.