TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Categories and sub categories - recursion (http://www.talkphp.com/advanced-php-programming/3991-categories-sub-categories-recursion.html)

Dinky 02-21-2009 06:11 PM

Categories and sub categories - recursion
 
Hi.

Im new here, so hello to eveybody :)

Im at my wits end. I can make a perfectly working recursion script that output my categories and sub categories with no problem.

But making the categories be outputted in an un-ordered list (CSS) doesn't work.


DATABASE

Code:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `sortorder` int(11) NOT NULL,
  `status` enum('1','0') NOT NULL,
  `type` int(11) NOT NULL,
  `parent` int(11) NOT NULL default '0',
  `picture` varchar(150) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=106 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE `categories_description` (
  `id` int(11) NOT NULL auto_increment,
  `category_id` int(11) NOT NULL,
  `country_id` int(11) NOT NULL,
  `status` enum('1','0') NOT NULL,
  `name` varchar(70) NOT NULL,
  `description` text NOT NULL,
  `seo_title` text NOT NULL,
  `seo_description` text NOT NULL,
  `seo_keywords` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=106 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


The structure basically uses:

ID : Unique identification
PARENT : Referer to the parent category

The rest is just various data that I use for other things.


Recursion function

PHP Code:

class Categories {
    public function 
categories_tree($country_id='',$parent='0',$spacer='') {
        
$db = new Db();
        
$sql $db->query("SELECT c.*,cd.name FROM categories AS c,categories_description AS cd WHERE c.id=cd.category_id AND c.parent='$parent' AND cd.country_id='$country_id' ORDER BY c.parent,c.type,c.sortorder");
        
$num $db->count_rows($sql);
        
$spacer .= '- ';
        while(
$row $db->fetchArray($sql)) {
            if(
$num==|| $row['parent']=='0') {
                
$spacer '';
            }
            echo 
$spacer.'<a href="productlisting.php?cid='.$row['id'].'">'.$row['name'].'</a><br />';
            
$this->categories_tree($country_id,$row['id'],$spacer);
        }
    }


All this will output e.g.:

Category 1
- Sub Category 2
- Sub Category 3
- - Sub Category 4
Category 5
- Sub Category 6

What I need is:

HTML Code:

<ul id="categories" class="treeview">
        <li><span>Category 1</span>
                <ul>
                        <li><span>Sub Category 2</span></li>
                        <li><span>Sub Category 3</span>
                                <ul>
                                        <li><span>Sub Category 4</span></li>
                                </ul>
                        </li>
                </ul>
        </li>
        <li><span>Category 5</span>
                <ul>
                        <li><span>Sub Category 6</span></li>
                </ul>
        </li>
</ul>

This code will not work.

Because the ending </li> and </ul> will be missing the right spots.

PHP Code:

class Categories {
    public function 
categories_tree($country_id='',$parent='0',$spacer='') {
        
$db = new Db();
        
$sql $db->query("SELECT c.*,cd.name FROM categories AS c,categories_description AS cd WHERE c.id=cd.category_id AND c.parent='$parent' AND cd.country_id='$country_id' ORDER BY c.parent,c.type,c.sortorder");
        
$num $db->count_rows($sql);
        
$spacer .= '- ';
        while(
$row $db->fetchArray($sql)) {
            if(
$num==|| $row['parent']=='0') {
                
$spacer '';
            }
            echo 
'<ul><li><span>'.$spacer.'<a href="productlisting.php?cid='.$row['id'].'">'.$row['name'].'</a></span>';
            
//echo $spacer.'<a href="productlisting.php?cid='.$row['id'].'">'.$row['name'].'</a><br />';
            
$this->categories_tree($country_id,$row['id'],$spacer);
            echo 
'</li></ul>';
        }
    }



jlodell 02-22-2009 07:00 AM

Try moving the ul tag outside the loop. That should correctly render the list.
You are looping thru the items that belong in the list, the recursive call will take care of wrapping the items in the ul.
i didn't check the logic just moved the ul tag.
<code>
class Categories {
public function categories_tree($country_id='',$parent='0',$spacer ='') {
$db = new Db();
$sql = $db->query("SELECT c.*,cd.name ".
"FROM categories AS c,categories_description AS cd ".
"WHERE c.id=cd.category_id ".
"AND c.parent='$parent' ".
"AND cd.country_id='$country_id' ".
"ORDER BY c.parent,c.type,c.sortorder");
$num = $db->count_rows($sql);
$spacer .= '- ';
echo '<ul>';
while(FALSE !== ($row = $db->fetchArray($sql))){
if($num==0 || $row['parent']=='0') {
$spacer = '';
}
echo '<li><span>',
$spacer,
'<a href="productlisting.php?cid=',
$row['id'],
'">',
$row['name'],
'</a></span>';

$this->categories_tree($country_id,$row['id'],$spacer);
echo '</li>';
}
echo '</ul>';
}
}
</code>

mombunheng 02-01-2010 07:45 PM

it what I currently looking for as well, can any one help. I am currently use on my project http://www.wcscambodia.org, but wrote in long php code.


All times are GMT. The time now is 03:08 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0