TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-21-2009, 06:11 PM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Dinky is on a distinguished road
Default 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>';
        }
    }

Dinky is offline  
Reply With Quote
Old 02-22-2009, 07:00 AM   #2 (permalink)
The Wanderer
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
jlodell is on a distinguished road
Default

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>
jlodell is offline  
Reply With Quote
Old 02-01-2010, 07:45 PM   #3 (permalink)
The Wanderer
 
mombunheng's Avatar
 
Join Date: Oct 2009
Posts: 5
Thanks: 0
mombunheng is on a distinguished road
Default

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.
mombunheng is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:38 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design