title: PHP MySQL Multidimensional Array - Dropdown Menu
body: <p>I want to create my dropdown menu from a mysql query, but i'm having trouble with the sub-items.</p>
  
  <p>My basic table:</p>
  
  <pre><code>NavigationID  ParentID  Name       Url
  1             1         Home       home
  2             2         About      about
  3             3         Products   products
  4             3         Category1  #
  5             3         Category2  #
  6             4         Product1   #
  7             5         Product2   #
  </code></pre>
  
  <p>My simple MySQL Query and adding to array:</p>
  
  <pre><code>class Navigation{
      private $data;
  
      public function __construct($par){
          if(is_array($par))
              $this-&gt;data = $par;
      }
  
      public function __toString(){
          return '&lt;li&gt;&lt;a href="'.$this-&gt;data['Url'].'"&gt;'.$this-&gt;data['Name'].'&lt;/a&gt;&lt;/li&gt;';
      }
  }
  
  $query = mysql_query("SELECT * FROM Navigation n") or die(mysql_error());
  $num = mysql_num_rows($query);
  $menuitems = array();
  
  while($row = mysql_fetch_assoc($query)){
      $menuitems[] = new Navigation($row);
  }
  
  echo '&lt;div id="nav"&gt;&lt;ul&gt;';
  foreach($menuitems as $item){
      echo $item;
  }
  echo '&lt;/ul&gt;&lt;/div&gt;';
  </code></pre>
  
  <p>The result of this is:</p>
  
  <pre><code>&lt;div id="nav"&gt;&lt;ul&gt;
     &lt;li&gt;&lt;a href="home"&gt;Home&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="about"&gt;About&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="products"&gt;Products&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="#"&gt;Category1&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="#"&gt;Category2&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="#"&gt;Product1&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="#"&gt;Product2&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;&lt;/div&gt;
  </code></pre>
  
  <p>But what I would REALLY like is this:</p>
  
  <pre><code>&lt;div id="nav"&gt;&lt;ul&gt;
     &lt;li&gt;&lt;a href="home"&gt;Home&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="about"&gt;About&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="products"&gt;Products&lt;/a&gt;
         &lt;ul&gt;
            &lt;li&gt;&lt;a href="#"&gt;Category1&lt;/a&gt;
                &lt;ul&gt;
                   &lt;li&gt;&lt;a href="#"&gt;Product1&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;Category2&lt;/a&gt;
                &lt;ul&gt;
                   &lt;li&gt;&lt;a href="#"&gt;Product2&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
     &lt;/li&gt;
  &lt;/ul&gt;&lt;/div&gt;
  </code></pre>
  
  <p>How can I achieve this result?  I've tried many other examples, but none seems to help me.  Maybe I'm not searching for the right thing.</p>