Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

tree menu from database - 3 levels

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

tree menu from database - 3 levels

Postby wasssu » Sat Sep 29, 2012 2:19 am


I want to build a tree menu (table with categories on 3 level) from database.

I want to look like this using unsorted lists (3 levels):
-Subcategory 1
-Subcategory 2
-Sub-Subcategory 1
-Sub-Subcategory 1

-Subcategory 1
-Subcategory 2
-Subcategory 3
-Sub-Subcategory 1
-Sub-Subcategory 1
-Subcategory 4

The structure of the Category table is like this:

So i put from database into an array():
Code: Select all
$menu = Array();
while ($m = mysql_fetch_array($result)) {
$menu[] = Array('id'=>$m['category_id'], 'parent'=>$m['parent_id']);

But from here i've tried different algorithms but it does't work.

I have only 2 main categories:
category_id = 207
parent_is = 0

category_id = 286
parent_is = 0

And then sub categories and sub subcategories

Could you please help me with this?

Thank you!!
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Sep 29, 2012 2:17 am

Re: tree menu from database - 3 levels

Postby schwarzenneger » Wed Feb 13, 2013 12:40 pm

Seems like you're using Adjacency List Model. 3 leveks or n levels -- you can use recursion to iterate and display the tree data. It is explained here:

Traversing Hierarchy Tree Using PHP: Adjacency List Model
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Oct 11, 2012 12:11 am

Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.