title: Generating Unordered List with PHP + CodeIgniter from a MySQL Database
body: <p>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.</p>
  
  <p>This is the end output I need to achieve.</p>
  
  <pre><code>&lt;ul id="categories" class="menu"&gt;
  &lt;li rel="1"&gt;
      Arts &amp;amp; Humanities
      &lt;ul&gt;
          &lt;li rel="2"&gt;
              Photography
              &lt;ul&gt;
                  &lt;li rel="3"&gt;
                      3D
                  &lt;/li&gt;
                  &lt;li rel="4"&gt;
                      Digital
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li rel="5"&gt;
              History
          &lt;/li&gt;
          &lt;li rel="6"&gt;
                          Literature
          &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li rel="7"&gt;
      Business &amp;amp; Economy
  &lt;/li&gt;
  &lt;li rel="8"&gt;
      Computers &amp;amp; Internet
  &lt;/li&gt;
  &lt;li rel="9"&gt;
      Education
  &lt;/li&gt;
  &lt;li rel="11"&gt;
      Entertainment
      &lt;ul&gt;
          &lt;li rel="12"&gt;
              Movies
          &lt;/li&gt;
          &lt;li rel="13"&gt;
              TV Shows
          &lt;/li&gt;
          &lt;li rel="14"&gt;
              Music
          &lt;/li&gt;
          &lt;li rel="15"&gt;
              Humor
          &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li rel="10"&gt;
      Health
  &lt;/li&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is my SQL that I have to work with.</p>
  
  <pre><code>--
  -- 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 ;
  </code></pre>
  
  <p>So I know that I am going to need at least 1 foreach loop to generate the first level of categories.</p>
  
  <p>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.</p>
  
  <p>Thanks for any help you can offer.</p>
  
  <p>Tim</p>