Re: Debug recursion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/23/07, kioto <kiotoster@xxxxxxxxx> 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>';
--
View this message in context: http://www.nabble.com/Debug-recursion-tf3632803.html#a10143940
Sent from the PHP - General mailing list archive at Nabble.com.

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


By using this class it is overwriting the result resource within your
recursion since you are using it as a global.

A "better way" (faster at any rate) to accomplish what you're trying
to do is to keep your data in a parent-child relationship for your
updates, deletes, and inserts.  Use the modified tree traversal for
your selects.  Just make sure to regenerate the left/right positions
on any data changes.

I've found this gets the ease of use for simple inserting and removing
records while the speed of a single query for your reads.  This is how
most of the traffic on the site will probably work anyways.  Take a
look at http://www.sitepoint.com/article/hierarchical-data-database/2
as it gives most of the code you'd need to get the project working.

Just remember by using recursive functions like the one you are trying
you will end up with tons of queries for no reason which will bring
the site to a crawl if you get too much data in there or a lot of
concurrent requests.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux