I have a table with the following structure:
cat_id (pk)
parent_cat_id
cat_desc
With the above mentioned struct we can go for a unlimited multilevel category list. This is applicable for either a ecomm, knowledge base, faq etc kinda site.
I know I have to use recursive function to get this done like the following:
Category 1
- sub cat 1
-- sub sub cat 1
-- sub sub cat 2
- sub cat 2
-- sub sub cat 3
Category 2
- sub cat 3
-- sub sub cat 4
-- sub sub cat 5
Please help me out with the idea/concept or example code re how to do this.
Unlimited multilevel category list with PHP/MySQL
Moderator: General Moderators
-
mukherjiikunal
- Forum Newbie
- Posts: 3
- Joined: Mon Dec 01, 2008 1:34 am
Re: Unlimited multilevel category list with PHP/MySQL
- Get list of children with a specific parent
- For each child (assuming there are any)
- Do what you want with it (like, display it)
- Repeat step 1
-
mukherjiikunal
- Forum Newbie
- Posts: 3
- Joined: Mon Dec 01, 2008 1:34 am
Re: Unlimited multilevel category list with PHP/MySQL
I know the concept already. Would appreciate some sample code please!
Re: Unlimited multilevel category list with PHP/MySQL
Well then, let's see what I can come up with.
Code: Select all
function recursive($num, $indent = "") {- Get list of children with a specific parent
Code: Select all
$children = mysql_query("SELECT num, name FROM table WHERE parent=$num"); - For each child (assuming there are any)
Code: Select all
while ($child = mysql_fetch_array($children)) {- Do what you want with it (like, display it)
Code: Select all
echo $indent, " ", $child["name"], "<br/>\n"; - Repeat step 1
Code: Select all
recursive($child["num"], "--$indent");
Code: Select all
} - Do what you want with it (like, display it)
Code: Select all
}-
mukherjiikunal
- Forum Newbie
- Posts: 3
- Joined: Mon Dec 01, 2008 1:34 am
Re: Unlimited multilevel category list with PHP/MySQL
Thanks a lot for your kind help. Will try the code and revert back.
Many thanks.
Many thanks.