kioto wrote:
Hi all i have found a bug in my db class when i use the recursion.
I try to use the adjacency list model to develop a three menu but
when i call the function in recursive way i loose data because
the value returned from the fetch seem to be empty.
I have db table like this:
table catalog
ID | Name_Category | Subcategory
1 node category 0
2 1_sub_category 1
3 2_sub_category 1
4 another_node 0
5 another_node 0


I have this db class code:
http://phpfi.com/229087
And i create an instance of such class with this code.
http://phpfi.com/229088
My problem is that i take only the fist main category, the subcategory of
this node and later the function esc and doesn't print the other main
category.
I have try to use the native php mysql function and the code work
then the problem i suppose is in my class.

Procedural way with native functions

unction buildThree($parent)
{
        $sql = "SELECT id, name_category FROM category WHERE subcategory =
{$parent}";
        $rs = mysql_query($sql) or die(mysql_error());
        
        if ($rs) {
                while (list($id, $nome) = mysql_fetch_array($rs)) {
                        $sql2 = "SELECT id FROM category WHERE subcategory = 
{$id}";
                        $rs2 = mysql_query($sql2) or die(mysql_errno());
                        
                        $total = mysql_num_rows($rs2);
                        
                        if ($total) {
                                echo'<li> # '.$nome.' '."\n\r".'<ul>'."\n\r";
                                buildThree($id);
                                echo"</ul>"."\n\r"."</li>"."\n\r";
                        } else {
                                echo'<li> ?m=product&amp;cat='.$id.' '.$nome.' 
</li>'."\n\r";
                        }
                }       
        }
}


echo '<ul>';
buildThree(0);
echo '</ul>';



echo '<ul>';
buildThree(0);
echo '</ul>';
ok, after playing with this for a little bit, I think I have come up with something that will work for you.

<?php

$sql = 'SELECT id, name_category, subcategory FROM category ORDER BY 
subcategory, id';
$rs = mysql_query($sql) or die(mysql_error());

$d = array();
while ( $row = mysql_fetch_assoc($rs) ) {
        $d[$row['subcategory']][] = array('id' => $row['id'], 'name' => 
$row['name_category']);
}

function buildTree(&$d, $parent) {
        if ( isset($d[$parent]) ) {
                echo "<ul>\n";
                foreach ($d[$parent] AS $subList) {
        echo "<li> # <a 
href='?category_id={$subList['id']}'>{$subList['name']}</a>\n";
                        if ( buildTree($d, $subList['id']) ) {
                                unset($d[$subList['id']]);
                        }
                        echo "</li>\n";
                }       
                echo "</ul>\n";
                return true;
        }
        return false;
}

# This is an example data set.
# The SQL output from above should resemble this layout if you were to do a 
print_r() on it.
# Take out the following 3 array definitions and you should have what you are 
looking for
$d[0]   = array(
                array(
                        'id'    => 1,
                        'name'  => 'Home',
                        ),
                array(
                        'id'    => 10,
                        'name'  => 'Programming',
                        ),
                array(
                        'id'    => 13,
                        'name'  => 'Music Bands',
                        ),
                );
$d[10]  = array(
                array(
                        'id'    => 85,
                        'name'  => 'PHP',
                        ),
                array(
                        'id'    => 86,
                        'name'  => 'Ruby',
                        ),
                );
$d[13]  = array(
                array(
                        'id'    => 163,
                        'name'  => 'Rush',
                        ),
                array(
                        'id'    => 121,
                        'name'  => 'Pink',
                        ),
                );


buildTree($d, 0);

?>


--
Enjoy,

Jim Lucas

Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree.

- Rush

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to