Creating a multi-dimensional array from MySQL table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noginn
Forum Newbie
Posts: 4
Joined: Fri Jul 15, 2005 3:58 pm

Creating a multi-dimensional array from MySQL table

Post by noginn »

Hi, I need to create a multi-dimensional array from a database table for a navigation tree system.

Code: Select all

CREATE TABLE `navigation` (
  `nav_id` int(11) NOT NULL auto_increment,
  `nav_parent` int(11) NOT NULL default '0',
  `nav_label` varchar(50) NOT NULL default '',
  `nav_href` varchar(255) NOT NULL default '',
  `nav_position` int(11) NOT NULL default '0',
  PRIMARY KEY  (`nav_id`)
) TYPE=MyISAM;
nav_id is the ID of the navigation item
nav_parent is the ID of the navigation item which is the current nav items parent (if that makes sense? :p)

That is the table format.

I need to output it to build a navigation tree like this;

Item1
-Item2
--Item3
--Item4
--Item5
---Item6
--Item7
-Item8
-Item9
Item10
Item11
-Item12

and so on.

Only managed to get 2 level array right so far. Any help is much appreciated.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

do you really have to use a multi-d array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as I've posted previously, this may help you create the n-dimensional array. At the very least, it should help give a step in that direction.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

i dont know whether this will help you :?:
get all parent elements
start loop
get all child elements
start loop
add href and styling(a tab???) for each child element
end loop
end loop
Post Reply