[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 03-04-2007, 06:10 PM   #1 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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, 08:12 PM   #2 (permalink)
NamePros Regular
 
vinodkv's Avatar
 
Join Date: Feb 2007
Posts: 403
190.75 NP$ (Donate)

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, 08:26 PM   #3 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


awesome thanks
unknowngiver is offline  
Old 03-05-2007, 02:39 PM   #4 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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($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=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 02:48 PM.
unknowngiver is offline  
Old 03-06-2007, 12:07 AM   #5 (permalink)
cef
NamePros Regular
 
Join Date: May 2004
Location: NYC
Posts: 236
76.50 NP$ (Donate)

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)
(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

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

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


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:13 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85