Generating Unordered List with PHP + CodeIgniter from a MySQL Database
- by Tim
Hello Everyone,
I am trying to build a dynamically generated unordered list in the following format using PHP. I am using CodeIgniter but it can just be normal php.
This is the end output I need to achieve.
<ul id="categories" class="menu">
<li rel="1">
Arts & Humanities
<ul>
<li rel="2">
Photography
<ul>
<li rel="3">
3D
</li>
<li rel="4">
Digital
</li>
</ul>
</li>
<li rel="5">
History
</li>
<li rel="6">
Literature
</li>
</ul>
</li>
<li rel="7">
Business & Economy
</li>
<li rel="8">
Computers & Internet
</li>
<li rel="9">
Education
</li>
<li rel="11">
Entertainment
<ul>
<li rel="12">
Movies
</li>
<li rel="13">
TV Shows
</li>
<li rel="14">
Music
</li>
<li rel="15">
Humor
</li>
</ul>
</li>
<li rel="10">
Health
</li>
And here is my SQL that I have to work with.
--
-- Table structure for table `categories`
--
CREATE TABLE IF NOT EXISTS `categories` (
`id` mediumint(8) NOT NULL auto_increment,
`dd_id` mediumint(8) NOT NULL,
`parent_id` mediumint(8) NOT NULL,
`cat_name` varchar(256) NOT NULL,
`cat_order` smallint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So I know that I am going to need at least 1 foreach loop to generate the first level of categories.
What I don't know is how to iterate inside each loop and check for parents and do that in a dynamic way so that there could be an endless tree of children.
Thanks for any help you can offer.
Tim