DrDankWD Posted June 26, 2008 Share Posted June 26, 2008 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 More sharing options...
lemmin Posted June 26, 2008 Share Posted June 26, 2008 I think if you join the table with itself on the parent id it will keep getting rows of the sub pages. Then you don't need the second while loop. Link to comment https://forums.phpfreaks.com/topic/112111-solved-nested-while-statements/#findComment-575575 Share on other sites More sharing options...
DrDankWD Posted June 27, 2008 Author Share Posted June 27, 2008 Awesome I think that will work!! I didn't even think of joining the table with itself. Thanks for the quick reply!! Link to comment https://forums.phpfreaks.com/topic/112111-solved-nested-while-statements/#findComment-575584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.