Jump to content

while inside while not working WHY???!


jwk811

Recommended Posts

why wont this work?? the labels are working but the options arent with the second while loop.

 

	$sql = "SELECT cat_id, cat_name FROM forum_category WHERE cat_parent_id = '0'";
$result = dbQuery($sql);

while($row = dbFetchAssoc($result)){
	$cat_id = $row['cat_id'];
	$cat_name = $row['cat_name'];

	echo "<optgroup label=\"$cat_name\">";

	$sql2 = "SELECT cat_id AS sub_cat_id, cat_name AS sub_cat_name FROM forum_category WHERE cat_parent_id = 10";
	$result2 = dbQuery($sql);

	while($row2 = dbFetchAssoc($result2)){
		$sub_cat_id = $row2['sub_cat_id'];
		$sub_cat_name = $row2['sub_cat_name'];

		echo "<option $selected value=\"$sub_cat_id\">$sub_cat_name</option>";
	}
	echo "</optgroup>";
}

Link to comment
https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/
Share on other sites

siric is correct, there is no reason you cannot put a loop within another loop. However, in this case, the inside WHILE loop is completely unnecessary and is just a complete waste. The loop is processign the exact same records every time. I think you meant to get the sub-category options respective to each parent option.

 

But, the real reason you are not getting any results is you are using the first query when you meant to run the second query

$result2 = dbQuery($sql); //<< you menat to use $sql2

 

But, you can get all the data you need with a single query - which would be a much better approach.

THANK YOU!!!! :D

 

siric is correct, there is no reason you cannot put a loop within another loop. However, in this case, the inside WHILE loop is completely unnecessary and is just a complete waste. The loop is processign the exact same records every time. I think you meant to get the sub-category options respective to each parent option.

 

But, the real reason you are not getting any results is you are using the first query when you meant to run the second query

$result2 = dbQuery($sql); //<< you menat to use $sql2

 

But, you can get all the data you need with a single query - which would be a much better approach.

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.