Jump to content

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.

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.