views:

77

answers:

1

Hello guys,

I would like to write a recursive PHP function to retrive all the children for the specified category. I tried the one described here but it didn't output what I have expected.

My categories table looks like this:

CREATE TABLE `categories` (
 `category_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
 `category_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `category_slug` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `category_parent` smallint(5) unsigned NOT NULL DEFAULT '0',
 `category_description_ro` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `category_description_en` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`category_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1

Bellow is an example of data in the table:

category id | category name | category_parent

1            Categoria 1            0        
2            Categoria 2            0        
3            Categoria 3            0        
4            Categoria 1.1          1        
5            Categoria 1.2          1        
6            Categoria 1.3          1        
7            Categoria 1.1.2        4 

Thanks.

+2  A: 
function get_categories($parent = 0)
{
    $html = '<ul>';
    $query = mysql_query("SELECT * FROM `categories` WHERE `category_parent` = '$parent'");
    while($row = mysql_fetch_assoc($query))
    {
        $current_id = $row['category_id'];
        $html .= '<li>' . $row['category_name'];
        $has_sub = NULL;
        $has_sub = mysql_num_rows(mysql_query("SELECT COUNT(`category_parent`) FROM `categories` WHERE `category_parent` = '$current_id'"));
        if($has_sub)
        {
            $html .= get_categories($current_id);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}

print get_categories();
fabrik
@Salman A: I tried your example, but it only returns the first category.
Psyche
It isn't Salman's mistake (thanks for the edit, Salman). Modifying the code i think i know where's the error.
fabrik
@fabrik: it's a little better after that change, but I still can't get the full tree. It stops at "Categoria 1.1.2".
Psyche
Please give me a moment i'll sort out.
fabrik
The code above is working for me (except that my SQL queries isn't the same). Was any PHP/MySQL error there? I modified the row that checks $has_subs maybe it will be fine.
fabrik
@fabrik: so you can get the full tree with that code? Still doesn't work for me. There's no error, but the output stops at "Categoria 1.1.2".
Psyche
How many rows do you have in your table? How many rows doesn't displayed?
fabrik
@fabrik: I have the categories listed in my post. So 7 categories and it only displays "Categoria 1", "Categoria 1.1" and "Categoria 1.1.2".
Psyche
Allright, that means the snippet isn't detecting child nodes. Because $current_id must be right there's something wrong with my $has_sub query. Modified my post at this query hope it will work.
fabrik
@fabrik: still not working :(
Psyche
Can you tell me what's the output of "SELECT COUNT(`category_parent`) FROM `categories` WHERE `category_parent` = 1"?
fabrik
Hmm, this is strange. I have a MySQL singleton class for database connection and operations. I wrote the code again using plain MySQL functions and it works.
Psyche
Great! :) So the recursion hasn't worked because of singleton?
fabrik
Seems so... Thanks for the help.
Psyche
You're welcome :)
fabrik
I do wonder why isn't it working with that MySQL class I have?
Psyche
Even it doesn't calling you MySQL methods?
fabrik
If I use plain MySQL functions it works. If I use that singleton MySQL class, it doesn't.
Psyche