trouble with nested while loops
Posted: Tue Dec 05, 2006 11:09 am
feyd | Please use
what the code above does is displays the ingredients in a certain dish (which is what $ingredientsquery does - dish id has been passed fom a previous page). this bit works fine. using the outer while loop each ingredients is displayed and its id is copied into the $ingid variable each iteration.
the $getnutrition query is run fine because "success" is printed after every ingredient is.
The next part is where the problem is....
i want to copy every row returned in the $getnutrition query into the $fat variable then print the fat variable which should outputput something liike:
SPAGHETTI
success
40
obviouly spaghetti and 40 will change with each iteration of the outer loop
however no fat value is printed to the screen. does anyone know what i'm doing wrong? Hope this problem makes sense!
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
hi.
i have the following php method:Code: Select all
<td>
<?php
//get names of ingredients in given dish
$ingredientsquery = @mysql_query("SELECT ingredients.ingid, ingredients.name FROM ingredients INNER JOIN recipes ON recipes.ingid = ingredients.ingid WHERE recipes.dishid='$dishid'");
//error checking
if (!$ingredientsquery)
{
echo '<p>error</p>';
}
//copy rows into variables
while ($row = mysql_fetch_array($ingredientsquery))
{
//add database rows into variables. starts with first record then the next one every loop
$ingname = $row['name'];
$ingid = $row['ingid'];
echo '' . $ingname . '<br />';
//select fat from database of current ingid
$getnutrition = @mysql_query("SELECT fat FROM ingredients WHERE ingid = '. ingid .'");
if (!getnutrition)
{
echo'<p>error1</p>';
}
else
{
echo'<p>success</p>';
}
while($row = mysql_fetch_array($getnutrition));
{
$fat = $row['fat'];
echo '' . $fat . '<br />';
}
}
?>
</td>the $getnutrition query is run fine because "success" is printed after every ingredient is.
The next part is where the problem is....
i want to copy every row returned in the $getnutrition query into the $fat variable then print the fat variable which should outputput something liike:
SPAGHETTI
success
40
obviouly spaghetti and 40 will change with each iteration of the outer loop
however no fat value is printed to the screen. does anyone know what i'm doing wrong? Hope this problem makes sense!
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]