Jump to content


Photo

Categories with Sub-Categories tree, need help


  • Please log in to reply
8 replies to this topic

#1 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 06:58 PM

Hey all

I am making a category system, but cant quite figure out how to code an unlimited array of sub-categories, and also how to display them. in reality it probably wont go more than 5 or so deep, but it would be nice to make it infinite just incase

the categories table has 3 fields. cat_id, cat_name, parent_id (the cat_id of the category it's in)

if someone could just point me in the right direction i should be cool

cheers  ;D

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 September 2006 - 07:00 PM

for loop
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 07:11 PM

lol ok so maybe a little more descriptuive than that

any idea how to build the array of categories etc? anyone else done what i am looking to do could lend some pointers?

#4 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 07:27 PM

I would like to know how to do this as well..  Great Question..  In my example I will have the categories for Tutorials such as Photoshop, Illustrator, PHP, ASP, CSS, JavaScript, ETC...

But then some of them may have sub categories such as for Photoshop might have..

Text Effects
Photo Manipulation
Brushing
etc...

That'd be a good thing to know..

#5 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 07:53 PM

if you just want categories with 1 sub-category then its easy

just multiple sub-cats r proving difficult

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 08:04 PM

You need a recursive function EG

<?php
include 'db.php';

function listSubCats ($parent, $level = 0) {

        $sql = "SELECT category_id, name 
                FROM categories
                WHERE parent = '$parent' ";
        $res = mysql_query($sql) or die(mysql_error());
        
        while (list($id, $name) = mysql_fetch_row($res)) {
        
            $indent = str_repeat('-- --', $level);
            
            echo "$indent $name<br />";
            
            listSubCats ($id, $level+1);     // now list subcats of this
        
        }
}

listSubCats(0);  // assuming top level have parent id = 0   
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 08:10 PM

ahh mate at first glance that looks perfect!!
gonna try it out. swear i tried putting a function inside itself before and it cried lol

anyways nice one :)

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 08:18 PM

Note - I wrote for my own test table and col names slightly different
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 08:21 PM

ahh dude you've only gone and done it!!

exactly what i was after, a nice little function, brilliant :)

cheers man!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users