Jump to content

[SOLVED] Nested while statements


DrDankWD

Recommended Posts

I'm stumped, im hoping someone has an idea...

 

I have a DB table:

  prSitemap_pages

 

With the fields:

  id

  parent_id

  title

  project_id

 

When a page has a parent_id of 0 it is a top level page, when a page has a parent_id of another pages id then it is a sub page.

 

There can be an unlimited number of subpages for each parent and subpage (sub sub pages, and sub sub sub sub pages, etc.)

 

When I am trying to create a display of all the pages and thier sub pages I am not sure how to go about nesting the loops.  Basically what I have is:

 

$project_id = $_GET['pId'];
$get_parents = db_query("SELECT * FROM prSitemap_pages WHERE project_id = '$project_id' AND parent_id = '0'");
while ($parents = mysql_fetch_array($get_parents)){
//print parent
echo $parents['title'].'<br />';		
            //get_children///
	$get_children1 = db_query("SELECT * FROM prSitemap_pages WHERE project_id = '$project_id' AND parent_id = '".$parents['id']."'");
	while ($children1 = mysql_fetch_array($get_children1)){
		//print the child
		echo $children1['title'].'<br />';

	}		
}

 

The above method would work if I knew how many layers down the sub pages would go.  I would just keep nesting another while statement into the previous until I had reached the fathest the sub pages could go.  But the issue is that the amount of sub pages layers could go down is unlimited.

 

I hope that makes sense, its been a long day :)

Link to comment
https://forums.phpfreaks.com/topic/112111-solved-nested-while-statements/
Share on other sites

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.