Page 1 of 1

Creating a multi-dimensional array from MySQL table

Posted: Fri Aug 19, 2005 8:09 am
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.

Posted: Fri Aug 19, 2005 8:11 am
by korto
do you really have to use a multi-d array?

Posted: Fri Aug 19, 2005 8:44 am
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.

Posted: Fri Aug 19, 2005 8:45 am
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