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.
Can't get my head round it!!!
Moderator: General Moderators
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.
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
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