PHP Pagestructure to array

sun49

New Member
Okey, so this is my problem. I have a page structure saved into a mysql db.Like this:Page 1
- SubPage 1.1
- - SubPage 1.1.1
- - - SubPage 1.1.1.1
- - - SubPage 1.1.1.2
- - SubPage 1.1.2
- SubPage 1.2
- SubPage 1.3
Page 2
Page 3The structure can have endless pages and sub pages. All pages have a field called "url" and "childof". "childof" is what binds a page as a subpage of another.Example:Page 1 have "url" page-1 and "childof" is empty
SubPage 1.1 have "url" subpage-1-1 and "childof" page-1
SubPage 1.1.1 have "url" subpage-1-1-1 and "childof" subpage-1-1 Hope you get the basic idea. My problem is to make a loop to get all these pages out in one good array.To get first line of pages is easy;\[code\]$sql = "SELECT * FROM `page` WHERE `childof` = '' ORDER BY `id` DESC";$result = mysql_query($sql);$i=0;while ($row = mysql_fetch_assoc($result)) { $pages[$i]['id'] = $row['id']; $i++;}\[/code\]To get second line is easy aswell...\[code\]for($x=0; $x < sizeof($pages); $x++){ $sql = "SELECT * FROM `page` WHERE `childof` = '".$pages[$x]['url']."' ORDER BY `id` DESC"; $result = mysql_query($sql); $i=0; while ($row = mysql_fetch_assoc($result)) { $pages[$x]['children'][$i]['id'] = $row['id']; $i++; }}\[/code\]And offcourse I could go on like this. But to do this without knowing how many subpages there are is not very efficient. So how do I make a loop to retrieve all pages and subpages into an array with a good structure?Thank you!
 
Back
Top