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
Share on other sites

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.