Recursing a tree menu
Moderator: General Moderators
Recursing a tree menu
Hi,
I am developing an application in PHP and MySQL. I need to display Cagtegories, Sub-Categories, etc in a drop down and it needs to be this in this format in the drop down:
Category1
SubCategory1
SubSubCategory1
SubCategory1_1
SubSubCategory1_1
Category2
Can anyone please help me with this?
Thanks
I am developing an application in PHP and MySQL. I need to display Cagtegories, Sub-Categories, etc in a drop down and it needs to be this in this format in the drop down:
Category1
SubCategory1
SubSubCategory1
SubCategory1_1
SubSubCategory1_1
Category2
Can anyone please help me with this?
Thanks
These need to be indented:
Code: Select all
Category1
SubCategory1
SubSubCategory1
SubCategory1_1
SubSubCategory1_1
Category2The table structure:
if its a Top Level Cateogry - the parent_id is 0
These needs to be populated in a drop down, with the indentation. Actually, a page lists links which are associated with a category, and if we need to change the category for a link, we can use the drop down to select another category/sub-cat and save.
I hope I am clear.
Thanks
Code: Select all
category_id parent_id category_name
1 0 Category 1
2 1 SubCategory1if its a Top Level Cateogry - the parent_id is 0
These needs to be populated in a drop down, with the indentation. Actually, a page lists links which are associated with a category, and if we need to change the category for a link, we can use the drop down to select another category/sub-cat and save.
I hope I am clear.
Thanks
I'd get all the categories & put them in a nested array - one that reflects the order you want & the heirarchy
ie: etc.
Then, loop through that array recursively, with each call to the function adding an indentation (with CSS or  ).
ie:
Code: Select all
[category1][subcategory1[subsubcategory1]
[subcategory2][subsubcategory1_1]Then, loop through that array recursively, with each call to the function adding an indentation (with CSS or  ).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
http://www.sitepoint.com/article/hierar ... a-database
Much nicer than using parent ids .. but a lot more complicated.
Much nicer than using parent ids .. but a lot more complicated.
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
i just finished this in just this fashion last night:
its done in ADODB so if it doesn't make sense check the manual out but i did two queries cuz i couldn't figure out how to do it in one. doing two queries is equivalent to making two tables FYI
********* code parser
pickle | language please
its done in ADODB so if it doesn't make sense check the manual out but i did two queries cuz i couldn't figure out how to do it in one. doing two queries is equivalent to making two tables FYI
Code: Select all
function showMainAndSubCategories
{
$queryStatement = "SELECT * FROM " . $this->pref . "forums where forum_parent =0";
$result = $this->sql->execute($queryStatement);
// this is until it's the end of the table
while (!$result->EOF)
{
this is the name of the main category
echo $result->fields['forum_name'].' <BR> ';
$queryStatement = " SELECT * FROM " . $this->pref . "forums where forum_parent = " .$result->fields['forum_id'];
$result2 = $this->sql->execute($queryStatement);
while (!$result2->EOF)
{
echo $result2->fields['forum_name'] . " <br>";
// move next one
$result2->moveNext();
}
// move next one
$result->moveNext();
}
}pickle | language please
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am