title: Convert array of paths into UL list
body: <p>I have a table in a database that contains a variety of paths to pages of my website. Each path is listed only one time. I currently have a very long and convoluted series of queries and PHP to pull all these and rewrite the data into an unordered list (to create a menu for my website). It seems that there is probably a relatively simple looping approach that would work MUCH more efficiently, but I cannot seem to get anything working.  I have found TONS of PHP scripts that create UL lists from file trees, but all of them either don't work or can't handle the inherently not recursive nature of my query results (some require multi-dimensional arrays of my paths, which would be fine except for my trouble with creating those). I did find a script that works pretty close, but it formats the <code>&lt;ul&gt;</code> portion incorrectly by placing sub-lists outside of the <code>&lt;li&gt;</code> section (I will explain below)</p>
  
  <p>Here is a sample:</p>
  
  <p>DB returns the following in results array:</p>
  
  <pre><code>about/contact/
  about/contact/form/
  about/history/
  about/staff/
  about/staff/bobjones/
  about/staff/sallymae/
  products/
  products/gifts/
  products/widgets/
  </code></pre>
  
  <p>and I want to create the following output:</p>
  
  <pre><code>&lt;ul&gt;
    &lt;li&gt;about/
    &lt;ul&gt;
      &lt;li&gt;about/contact/
      &lt;ul&gt;
        &lt;li&gt;about/contact/form/&lt;/li&gt;
      &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;about/history/&lt;/li&gt;
      &lt;li&gt;about/staff/
      &lt;ul&gt;
        &lt;li&gt;about/staff/bobjones/&lt;/li&gt;
        &lt;li&gt;about/staff/sallymae/&lt;/li&gt;
      &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;products/
    &lt;ul&gt;
      &lt;li&gt;products/gifts/&lt;/li&gt;
      &lt;li&gt;products/widgets/&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>So I got very close with a script found here:  <a href="http://www.daniweb.com/forums/thread285916.html" rel="nofollow">http://www.daniweb.com/forums/thread285916.html</a>  but I have run into a problem. It turns out that the script that I found creates improperly formatted UL lists. In a CORRECT situation, a sub-list is contained within the <code>&lt;li&gt;</code> of the parent element. In this scripting, the parent <code>&lt;li&gt;</code> is closed and then a <code>&lt;ul&gt;</code> block is inserted. The overall script is actually fairly elegant in the way that it keeps up with the levels and such, but I cannot wrap my head around it enough to figure out how to fix it. I have the whole thing in a function here:</p>
  
  <pre><code>function generateMainMenu()
  {
    global $db;
  
    $MenuListOutput = '';
    $PathsArray = array();
  
    $sql = "SELECT PageUrlName FROM `table`";
    $result = mysql_query($sql, $db) or die('MySQL error: ' . mysql_error());
    while ($PageDataArray = mysql_fetch_array($result))
    {
      $PathsArray[] = rtrim($PageDataArray['PageUrlName'],"/"); //this function does not like paths to end in a slash, so remove trailing slash before saving to array
    }
  
    sort($PathsArray);// These need to be sorted.
    $MenuListOutput .= '&lt;ul id="nav"&gt;'."\n";//get things started off right
    $directories=array ();
    $topmark=0;
    $submenu=0;
    foreach ($PathsArray as $value) {
      // break up each path into it's constituent directories
      $limb=explode("/",$value);
      for($i=0;$i&lt;count($limb);$i++) {
        if ($i+1==count($limb)){
          // It's the 'Leaf' of the tree, so it needs a link
          if ($topmark&gt;$i){
            // the previous path had more directories, therefore more Unordered Lists.
            $MenuListOutput .= str_repeat("&lt;/ul&gt;",$topmark-$i); // Close off the Unordered Lists
            $MenuListOutput .= "\n";// For neatness
          }
          $MenuListOutput .= '&lt;li&gt;&lt;a href="/'.$value.'"&gt;'.$limb[$i]."&lt;/a&gt;&lt;/li&gt;\n";// Print the Leaf link
          $topmark=$i;// Establish the number of directories in this path
        }else{
          // It's a directory
          if($directories[$i]!=$limb[$i]){
            // If the directory is the same as the previous path we are not interested.
            if ($topmark&gt;$i){// the previous path had more directories, therefore more Unordered Lists.
              $MenuListOutput .= str_repeat("&lt;/ul&gt;",$topmark-$i);// Close off the Unordered Lists
              $MenuListOutput .= "\n";// For neatness
            }
  
            // (next line replaced to avoid duplicate listing of each parent)
            //$MenuListOutput .= "&lt;li&gt;".$limb[$i]."&lt;/li&gt;\n&lt;ul&gt;\n";
            $MenuListOutput .= "&lt;ul&gt;\n";
            $submenu++;// Increment the dropdown.
            $directories[$i]=$limb[$i];// Mark it so that if the next path's directory in a similar position is the same, it won't be processed.
          }
        }
      }
    }
    $MenuListOutput .= str_repeat("&lt;/ul&gt;",$topmark+1);// Close off the Unordered Lists
  
    return $MenuListOutput."\n\n\n";
  }
  </code></pre>
  
  <p>and it returns something like this:</p>
  
  <pre><code>&lt;ul id="nav"&gt;
  &lt;li&gt;&lt;a href="/about"&gt;about&lt;/a&gt;&lt;/li&gt;
  &lt;ul&gt;
  &lt;li&gt;&lt;a href="/about/history"&gt;history&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="/about/job-opportunities"&gt;job-opportunities&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="/about/mission"&gt;mission&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="/about/privacy-policy"&gt;privacy-policy&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;li&gt;&lt;a href="/giftcards"&gt;giftcards&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="/locations"&gt;locations&lt;/a&gt;&lt;/li&gt;
  &lt;ul&gt;
  &lt;li&gt;&lt;a href="/locations/main-office"&gt;main-office&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="/locations/branch-office"&gt;branch-office&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;li&gt;&lt;a href="/packages"&gt;packages&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Anyone have an idea of where I need to add in some additional logic and how I can accomplish this? Other ideas on a better way to do this?  It seems like this is such a common issue that there would be a simple/standard method of handling something like this.  Maybe if I could figure out how to create a multi-dimensional array from my paths then those could be iterated to make this work?</p>
  
  <hr />
  
  <p><strong>EDIT: More Complex :-(</strong></p>
  
  <p>I tried casablanca's response and it worked perfectly...except I then realized that now I have a follow-up to make things more difficult. In order to display the "name" of the page, I need to also have that info in the array, thus the path probably works better as the array key and the name in the value. Any thoughts on changing like this:</p>
  
  <pre><code>$paths = array(
      "about/contact/ " =&gt; "Contact Us", 
      "about/contact/form/ " =&gt; "Contact Form",
      "about/history/ " =&gt; "Our History",
      "about/staff/ " =&gt; "Our Staff",
      "about/staff/bobjones/ " =&gt; "Bob",
      "about/staff/sallymae/ " =&gt; "Sally",
      "products/ " =&gt; "All Products",
      "products/gifts/ " =&gt; "Gift Ideas!",
      "products/widgets/ " =&gt; "Widgets"
  );
  </code></pre>
  
  <p>and then using something like this line within the <code>buildUL</code> function:</p>
  
  <pre><code>echo '&lt;a href="'.$prefix.$key.'/"&gt;'.$paths[$prefix.$key].'&lt;/a&gt;';
  </code></pre>