trouble with nested while loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

trouble with nested while loops

Post by slash_gnr3k »

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>
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]
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Your code is messed up:

Code: Select all

<?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 '' . $row['name'] . '<br />';
	
	//select fat from database of current ingid
	//************** If you start with double-quotes, you need to continue using them *******************// 
	$getnutrition = @mysql_query("SELECT fat FROM ingredients WHERE ingid = ".$row['ingid']);
	if (!$getnutrition)
	{
		echo'<p>error1</p>';
	}
	else
	{
		echo'<p>success</p>';
	}

	while($row = mysql_fetch_array($getnutrition));
	{
		echo '' . $row['fat'] . '<br />';
	}
}
?>
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

nested loops

Post by timclaason »

Whenever I do a SQL loop within another, I'll have a different array name.

For instance:

Code: Select all

$query = mysql_query("SELECT * FROM a", $db_link);
while($row = mysql_fetch_array($query)) {

  //Do some special query loop things
  $query2 = mysql_query("SELECT * FROM b WHERE field1='".$row['0']."'", $db_link);
  while($row2 = mysql_fetch_array($query2)) {
       //Now I don't have to worry about screwing up my $row[] variables, because the nested loop values are in $row2[]
  }


}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: nested loops

Post by ok »

[quote="timclaason"]Whenever I do a SQL loop within another, I'll have a different array name.

timclaason rights. I didn't notice it. You can't use the same name for the two arrays (it can cause a big mess).
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

its ok thanks guys. i sorted it a much simpler way. dont know why i didnt do it that way before!

@ok - sorry, noly just spotted that. was ok in the file ive been using. i just edited the code i posted here a little before i posted it to try and make it more clear

@timclasson & ok - thanks. didnt use a nested loop in the end but will bear in mind in future
Post Reply