Jump to content

[SOLVED] Looping a mysql? - More complicated than it sounds


tomtom

Recommended Posts

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.

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;
}
?>

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>";
    }
}
?>

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.