NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page categories sub categories Tree

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-04-2007, 07:10 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes

categories sub categories Tree


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
also..if possible..i wana use ajax in it...2 dynamically load sub categories..etc..not that biggie right now tho..
thanks
unknowngiver is offline  
Old 03-04-2007, 09:12 PM   #2 (permalink)
NamePros Regular
 
vinodkv's Avatar
Join Date: Feb 2007
Posts: 403
vinodkv has a spectacular aura aboutvinodkv has a spectacular aura about
 



check these ....

http://forums.devshed.com/php-develo...on-292257.html

http://forums.devshed.com/php-develo...ry+subcategory

http://dev.mysql.com/tech-resources/...ical-data.html

i think its will solve your problem....
__________________
Web Design Forum - - Free Web Directory
vinodkv is offline  
Old 03-04-2007, 09:26 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
awesome thanks
unknowngiver is offline  
Old 03-05-2007, 03:39 PM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
hey i followed the php recursion code..but i see a Blank page:S for some reason..heres the code
PHP Code:
<?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($queryMYSQL_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=MyISAM  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 by unknowngiver; 03-05-2007 at 03:48 PM.
unknowngiver is offline  
Old 03-06-2007, 01:07 AM   #5 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
change the catGen function declaration from

PHP Code:
function catGen($catID$cPath
to
PHP Code:
function catGen($catID, &$cPath
????: NamePros.com http://www.namepros.com/programming/301220-categories-sub-categories-tree.html
(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 Code:
$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.
cef is offline  
Closed Thread


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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 05:37 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger