tomtom Posted May 15, 2008 Share Posted May 15, 2008 Right. So basically. I want to make my site, so that each page can have a child page. So all the pages are stored in `pages`, and their parent (the page ABOVE it, in the tree)'s id. I want it, to be able to go on forever. And display it like a tree. parent id 4 parent id 0 child with parent id 4, id 6 childchild with parent id 6, id 7 childchild with parent id 6, id 9 child with parent id 4, id 32 parent id 124, parent id 0 if you get me? I've attempted to make a code for it. But it's hard because you want it to while(GET ALL THE PARENTS) { while(GET THEIR CHILD) { while(GET THEIR CHILDREN) { and so on, forever. unless, they don't have children. If you have any questions, ask me. My code... <?php $select_parents = mysql_query("SELECT * from `pages` WHERE `parent` = '0'"); // get all parents while($parent = mysql_fetch_array($select_parents)){ // look at each individual parent echo "<div style=\"position: relative; left: 30px;\">" . $parent[title] . "<P>"; $select_children = mysql_query("SELECT * from `pages` WHERE `parent` = '$parent[id]'"); // get first tier children $childcount = ""; while($children = mysql_fetch_array($select_children)){ // individual children.. *yawn* $childcount = "$childcount//$children[id]"; // build up $childcount to contain all children } $t=1; $childarray = explode("//",$childcount); // explode the childcount into arrays $child = $childarray[$t]; while(!empty($child)){ $select = mysql_query("SELECT * from `pages` WHERE `id` = '" . $child . "'"); while($nav = mysql_fetch_array($select)) { echo "<div style=\"position: relative; left: 30px;\">" . $nav[title]; if($nav[id]!=$childarray[$last_one]) { $t=$t+1; $child = $childarray[$t]; }else{ $select = mysql_query("SELECT * from `pages` WHERE `parent` = '" . $nav[id] . "'"); while($page = mysql_fetch_array($select)) { $child = "$child//$page[id]"; } $explode = explode("//",$child); $count = count($explode)+1; $bla = $explode[$count]; $child = $bla; } echo "</div>"; } } echo "</div>"; } ?> So far the code works for all the parents, and getting their childs. But it doesnt go further than that. Quote Link to comment https://forums.phpfreaks.com/topic/105731-solved-looping-a-mysql-more-complicated-than-it-sounds/ Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 basically your need a function that calls itself, this is probably totally wrong but should give you an idea of what i mean <?php $select_parents = mysql_query("SELECT * from `pages` WHERE `parent` = '0'"); // get all parents while($parent = mysql_fetch_array($select_parents)){ // look at each individual parent echo "<div style=\"position: relative; left: 30px;\">" . $parent['title'] . "<P>"; $select_children = mysql_query("SELECT * from `pages` WHERE `parent` = '{$parent['id']}'"); // get first tier children $childcount = ""; while($children = mysql_fetch_array($select_children)){ // individual children.. *yawn* $childcount = "$childcount//{$children['id']}"; // build up $childcount to contain all children } $t=1; $childarray = explode("//",$childcount); // explode the childcount into arrays $child = $childarray[$t]; while(!empty($child)){ $select = mysql_query("SELECT * from `pages` WHERE `id` = '" . $child . "'"); while($nav = mysql_fetch_array($select)) { echo "<div style=\"position: relative; left: 30px;\">" . $nav['title']; if($nav['id']!=$childarray[$last_one]) { $t=$t+1; $child = $childarray[$t]; }else{ $child = BuildChild($child, $nav['id']); } echo "</div>"; } } echo "</div>"; } function BuildChild($child, $nav) { $select = mysql_query("SELECT * from `pages` WHERE `parent` = '" . $nav . "'"); while($page = mysql_fetch_array($select)) { $tmp = BuildChild($page['id']); $child = "$child//{$page['id']}//$tmp"; } $explode = explode("//",$child); $count = count($explode)+1; $bla = $explode[$count]; $child = $bla; return $child; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105731-solved-looping-a-mysql-more-complicated-than-it-sounds/#findComment-541742 Share on other sites More sharing options...
tomtom Posted May 16, 2008 Author Share Posted May 16, 2008 Yeah, I get what you mean. But I had tried that before and it hadn't seemed to work. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/105731-solved-looping-a-mysql-more-complicated-than-it-sounds/#findComment-542732 Share on other sites More sharing options...
tomtom Posted May 16, 2008 Author Share Posted May 16, 2008 Yeah, cheers about that I've fixed it now, and this code works... <?php $select_parents = mysql_query("SELECT * from `pages` WHERE `parent` = '0'"); while($parent = mysql_fetch_array($select_parents)){ echo "<div style=\"position: relative; left: 30px;\">" . $parent['title'] . "<br>"; $select_children = mysql_query("SELECT * from `pages` WHERE `parent` = '{$parent['id']}'"); $childcount = ""; while($children = mysql_fetch_array($select_children)){ echo "<div style=\"position: relative; left: 30px;\">" . $children['title'] . "<br>"; BuildChild($children[id]); echo "</div>"; } echo "</div>"; } function BuildChild($nav) { $select = mysql_query("SELECT * from `pages` WHERE `parent` = '" . $nav . "'"); while($page = mysql_fetch_array($select)){ echo "<div style=\"position: relative; left: 30px;\">" . $page['title'] . "<br>"; BuildChild($page[id]); echo "</div>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105731-solved-looping-a-mysql-more-complicated-than-it-sounds/#findComment-542738 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 Yes, something like that.. EDIT: Well done Solved? if so click solved please Quote Link to comment https://forums.phpfreaks.com/topic/105731-solved-looping-a-mysql-more-complicated-than-it-sounds/#findComment-542741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.