Jump to content

Looping within a loop?


Scip

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/161824-looping-within-a-loop/
Share on other sites

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

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.