jwk811 Posted May 5, 2010 Share Posted May 5, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/ Share on other sites More sharing options...
jamesxg1 Posted May 5, 2010 Share Posted May 5, 2010 A while cannot be inside another while loop, as foreach cannot to. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/#findComment-1053689 Share on other sites More sharing options...
siric Posted May 5, 2010 Share Posted May 5, 2010 A while cannot be inside another while loop, as foreach cannot to. Many thanks, James. James, I think you are mistaken - you can place a while loop within another while loop. Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/#findComment-1053699 Share on other sites More sharing options...
gwolgamott Posted May 5, 2010 Share Posted May 5, 2010 Need more info for what you are doing Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/#findComment-1053713 Share on other sites More sharing options...
Psycho Posted May 5, 2010 Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/#findComment-1053721 Share on other sites More sharing options...
jwk811 Posted May 6, 2010 Author Share Posted May 6, 2010 THANK YOU!!!! 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. Quote Link to comment https://forums.phpfreaks.com/topic/200813-while-inside-while-not-working-why/#findComment-1054186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.