NameSilo

Categories sub categories Tree

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
19
Hey
I am working on a website and im stuck on how to make a php categories and sub categories tree
i can load all the categories..but i dont know how to have sub categories under them
heres how my categories table look like:

Category
-id
-name
-parent
-status

I am looking for the best and efficient way of doing it..a script/tutorial..w.e u got :p
also..if possible..i wana use ajax in it...2 dynamically load sub categories..etc..not that biggie right now tho..
thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
0
•••
awesome:) thanks
 
0
•••
hey i followed the php recursion code..but i see a Blank page:S for some reason..heres the code
PHP:
<?php
include "include/database.php";
	function catGen($catID, $cPath)
	{
		if ($catID == 0) return;
		$sql = "SELECT `id`,`name`,`parent` FROM `category` WHERE `id` = {$catID} LIMIT 1"; 
		$query = mysql_query($sql) or trigger_error(mysql_errno() . ': ' . mysql_error()); 
		
		if (mysql_num_rows($query) > 0)
		{
			$result = mysql_fetch_array($query, MYSQL_ASSOC);
			$cPath[ $result['id'] ] = $result['name'];
			if ($result['parent'] > 0)
			{ 
				catGen($result['parent'], $cPath);
			}
			else
			{
				return; 
			} 
		}
		else
		{
			return; 
		}
	}

// START IT
//ERROR_REPORTING(E_ALL);
$catID = 1;
catGen($catID, $container);

print_r($container);
?>
and heres the mysql table
Code:
CREATE TABLE `category` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(25) NOT NULL default '',
  `parent` int(5) NOT NULL default '0',
  `status` smallint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

-- 
-- Dumping data for table `category`
-- 

INSERT INTO `category` VALUES (1, 'Funny Videos', 0, 1);
INSERT INTO `category` VALUES (2, 'Animation/Flash', 0, 1);
INSERT INTO `category` VALUES (4, 'Test1', 1, 0);
INSERT INTO `category` VALUES (5, 'Test2', 3, 0);
INSERT INTO `category` VALUES (6, 'abc', 1, 0);
 
Last edited:
0
•••
change the catGen function declaration from

PHP:
function catGen($catID, $cPath)
to
PHP:
function catGen($catID, &$cPath)

(put an '&' in front of $cPath)

This passes the parameter by reference, so that any changes you make to it are to the same instance of the $cPath variable, and they propagate back up the chain as you pop your recursion.

Also, you should declare an initial empty array before calling the function for the first time:

PHP:
$catID = 1;
$container = array();
catGen($catID, $container);
print_r($container);

Lastly, if you have a lot of nested categories, you might want to free your mysql query memory along the way with a call to mysql_free_result($query); right before you start recursing in the catGen function.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back