Scip Posted June 11, 2009 Share Posted June 11, 2009 Ok i have a slight problem. is it possible to do soemthing like this? $query="SELECT * FROM category"; $category_result=mysql_query($query,$connection); while($category=mysql_fetch_array($category_result){ //do some stuff $query="SELECT * FROM sub_category WHERE sub_category_id = {$category['id']}"; $sub_category_result=mysql_query($query,$connection); while($category=mysql_fetch_array($sub_category_result)){ //do some stuff mysql_data_seek($category_result,0); while($category=mysql_fetch_array($category_result){ // do some stuff like echo <option> $category['name']<option> } } } i tried the above but doesn't seem to work Quote Link to comment https://forums.phpfreaks.com/topic/161824-looping-within-a-loop/ Share on other sites More sharing options...
Quendian Posted June 11, 2009 Share Posted June 11, 2009 You can have loops inside loops, these are called embedded loops. (embedded for loop, embedded while loop etc) You have to be careful with what variables you change though because it will have adverse effects on the other loops like causing infinite loops etc. Quote Link to comment https://forums.phpfreaks.com/topic/161824-looping-within-a-loop/#findComment-853788 Share on other sites More sharing options...
RichardRotterdam Posted June 11, 2009 Share Posted June 11, 2009 In your code I see the following two queries query 1 SELECT * FROM category query 2 SELECT * FROM sub_category WHERE sub_category_id = {$category['id']} Aren't sub_category and category both the same? I think you might be better of using 1 category table instead of 2. then you can simply use 1 query instead Quote Link to comment https://forums.phpfreaks.com/topic/161824-looping-within-a-loop/#findComment-853789 Share on other sites More sharing options...
Scip Posted June 11, 2009 Author Share Posted June 11, 2009 Yeh but i want to loop through the results of the first query 'agian' in the second/embedded loop. Quote Link to comment https://forums.phpfreaks.com/topic/161824-looping-within-a-loop/#findComment-853811 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.