Page 1 of 1
Can't get my head round it!!!
Posted: Fri Jun 24, 2005 4:06 pm
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.
Posted: Fri Jun 24, 2005 4:08 pm
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..
Posted: Fri Jun 24, 2005 5:06 pm
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
Posted: Fri Jun 24, 2005 7:02 pm
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.
Posted: Sat Jun 25, 2005 10:18 am
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