Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Categories sub categories Tree

Spacemail by SpaceshipSpacemail by Spaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
0
Close
awesome:) thanks
 
0
Close
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
Close
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
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Search: choose where to search from the All tab
Open the search at the top of any page. On the All tab, the list next to the search box lets you choose where to search: everything, all...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back