tree menu from database - 3 levels

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

tree menu from database - 3 levels

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

Hi,

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):
Category1
-Subcategory 1
-Subcategory 2
-Sub-Subcategory 1
-Sub-Subcategory 1

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


The structure of the Category table is like this:
category_id
parent_id

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!!

schwarzenneger
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Oct 11, 2012 12:11 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


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 3 guests