Jump to content

trying to print $result in rows of a table


farahZ
Go to solution Solved by yomanny,

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++;
}
Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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);
}
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.