Can't get my head round it!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sid3335
Forum Newbie
Posts: 9
Joined: Fri Jun 24, 2005 4:00 pm

Can't get my head round it!!!

Post by Sid3335 »

I want code a page that allows you to add a category, e.g:

categories:
Cat1
Cat2

which also allows you to add unlimited sub-categories:
Cat1
SubCat1
SubCat2
SubCat3
SubCat4
Cat2
SubCat1

And continue adding them until you get bored. It will also show an updated list after every additional category.

How would i store this using code? it will have to be dynamically allocated, i'm not sure how php handles that. The simpler the better.

Thanks guys.
Sid3335
Forum Newbie
Posts: 9
Joined: Fri Jun 24, 2005 4:00 pm

Post by Sid3335 »

my text didn't indent but subcat 1-4 arent on the next level to Cat1, subcat2 is a subcat of subcat1, subcat3 is a subcat of subcat2 etc, etc..
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

1)create a table in your database named as nodes
2)each node have itemid, parentid, itemvalue properties
(it means you have 3 field in your table)
3)generate recursively every time page is loaded
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Probably time to do a websearch on "SQL tree". It will give you some info on some other techniques (nested sets, materialized path, ...) which don't require you to recurse.. If you look at http://pear.php.net you will even find some classes that implement the nested set solution.
Sid3335
Forum Newbie
Posts: 9
Joined: Fri Jun 24, 2005 4:00 pm

Post by Sid3335 »

Hi thanks for the replies. I don't have a database, i want to store the categories in code, but i've just started learning oop.

I have a class: (i have simplified it to contain only categories, no sub cats)

class Cat {
var $category;

function add_item($catName, $num) {
$this->category[$num]= $catName;
}
}

this is a seperate file and i include it in my other pages.
1 page simply outputs a category i tell it to, e.g;

$cat = new Cat;
echo $cat->category[0] ;

this page has a button that says "ADD CATEGORY" on it, this takes you to another page "add.php" and on this page i want to add another category. this page also includes the class file.

The problem is i can't add another category because i would have to create another instance of the class to add it.

how could i make it possible to keep adding categories to the class structure?
sorry i explain things poorly. thanks
Post Reply