title: build dynamic navigation with parent, child and child of child
body: <p>Having problem with building navigation for web site.</p>
  
  <p>This is the structure I am looking to generate:</p>
  
  <pre><code>&lt;nav id="navig"&gt;
      &lt;button id="menu-turner" onclick="return false;"&gt;Menu&lt;/button&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li class="dropable"&gt;&lt;a href="/samples/index.html"&gt;Samples&lt;/a&gt;&lt;!--href="/index.html"--&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="/samples/samples1.html"&gt;Samples1&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="/samples/samples2.html"&gt;Samples2&lt;/a&gt;&lt;/li&gt;
                      &lt;li class="dropable"&gt;&lt;a href="/samples/samples3.html"&gt;Samples3&lt;/a&gt;&lt;!--href="/quartz-samples.html"--&gt;
                      &lt;ul&gt;
                          &lt;li&gt;&lt;a href="/samples/samples3.1.html"&gt;Samples3.1&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="/samples/samples3.2.html"&gt;Samples3.2&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="/samples/samples3.3.html"&gt;Samples3.3&lt;/a&gt;&lt;/li&gt;
                      &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href="/quote/"&gt;Price&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="/recent-projects.html"&gt;Projects&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="/contact.html"&gt;Contact&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
  &lt;/nav&gt;
  </code></pre>
  
  <p>The table <code>pages</code> contains all information about pages, where top_nav_ID = ID on the table <code>top_nav</code>, which contains information about categories.</p>
  
  <p>Here is a structure of <code>pages</code>:</p>
  
  <pre><code>id | top_nav_id |parentpageID |menu_dropable|nav_anchor |link_show|pageURL
  _____________________________________________________________________________________
  1  | 1          | 0           | 0           |Home       |1        |index.html
  2  | 2          | 2           | 1           |Samples    |1        |index.html
  3  | 2          | 2           | 0           |Samples1   |1        |samples1.html
  4  | 2          | 2           | 0           |Samples2   |1        |samples2.html
  5  | 2          | 2           | 1           |Samples3   |1        |samples3.html
  6  | 2          | 2           | 0           |Samples3.1 |1        |samples3.1.html
  7  | 2          | 2           | 0           |Samples3.2 |1        |samples3.2.html
  8  | 2          | 2           | 0           |Samples3.3 |1        |samples3.3.html
  9  | 0          | 0           | 0           |Projects   |1        |recent-projects.html
  10 | 0          | 0           | 0           |Contact    |1        |contact.html
  11 | 0          | 0           | 0           |           |0        |search.html
  12 | 3          | 0           | 0           |Price      |1        |price.html
  </code></pre>
  
  <p>table <code>top_nav</code>:</p>
  
  <pre><code>ID | link_lbl | link_dir | link_url     | link_title | link_show
  _____________________________________________________________________
  1  | Home     |          |  index.html  | Home       | 1
  2  | Samples  | samples  |  index.html  | Samples    | 1
  3  | Price    | quote    |  index.html  | Price      | 1
  </code></pre>
  
  <p>Here is my PHP:</p>
  
  <pre><code>    &lt;?php
  print&lt;&lt;&lt;END
          &lt;nav id="navig"&gt;
              &lt;button id="menu-turner" onclick="return false;"&gt;Menu&lt;/button&gt;
                  &lt;ul&gt;\n
  END;
  
      $query = 'SELECT * FROM top_nav WHERE link_show = 1' ;
      $resp = $db-&gt;prepare($query);
      $resp-&gt;execute();
      while ($page_info = $resp-&gt;fetch()){
          $category_pageID = $page_info['ID'];
          $category_directory = $page_info['link_dir'];
  
          $pages = 'SELECT * FROM pages WHERE top_nav_ID = :top_nav_ID AND parentpageID = 0 AND link_show = 1 ORDER BY id asc';
          $res = $db-&gt;prepare($pages);
          $res-&gt;execute(array(":top_nav_ID" =&gt; $category_pageID));
          while ($info = $res -&gt; fetch()){
              if($category_pageID = $info['top_nav_ID']){
              $calss_dropable = ($info['menu_dropable'] == 0)? '' : ' class="dropable"' ;
  
                  $query = 'SELECT * FROM pages WHERE top_nav_ID = :top_nav_ID AND parentpageID &gt; 0 AND link_show = 1 ORDER BY id asc';
                  $res = $db-&gt;prepare($query);
                  $res-&gt;execute(array(":top_nav_ID" =&gt; $category_pageID));
                  while ($infor = $res -&gt; fetch()){
                      $subpageURL = Config::DS.$category_directory.Config::DS.$infor['pageURL'] ;
                      $readysubURL = '&lt;li&gt;&lt;a href="'.$subpageURL.'"&gt;'.$infor['nav_anchor'].'&lt;/a&gt;&lt;/li&gt;'."\n";
                  }
                  $list_tags = ($info['menu_dropable'] == 0)? '' :  "\n".'&lt;ul&gt;'."\n".$readysubURL.'&lt;/ul&gt;'."\n" ;
                  $pageURL = ($category_directory == NULL)? Config::DS.$info['pageURL'] : Config::DS.$category_directory.Config::DS.$info['pageURL'] ;
  
  
          $readyURL = '&lt;li'.$calss_dropable.'&gt;&lt;a href="'.$pageURL.'"&gt;'.$info['nav_anchor'].'&lt;/a&gt;'.$list_tags.'&lt;/li&gt;';
              }
          echo $readyURL."\n";
          }
      }
  
  
  $pages = 'SELECT * FROM pages WHERE link_show = 1 ORDER BY id asc';
  $res = $db-&gt;prepare($pages);
  $res-&gt;execute();
  
  while ($info = $res -&gt; fetch()){
  
      $top_nav_ID = $info['top_nav_ID'];
  
      if($top_nav_ID == 0){
          $pageURL = Config::DS.$info['pageURL'];
          $calss_dropable = ($info['menu_dropable'] == 0)? '' : ' class="dropable"' ;
          $list_tags = ($info['menu_dropable'] == 0)? '' :  '&lt;ul&gt;  &lt;/ul&gt;' ;
          $readyURL = '&lt;li'.$calss_dropable.'&gt;&lt;a href="'.$pageURL.'"&gt;'.$info['nav_anchor'].'&lt;/a&gt;'.$list_tags.'&lt;/li&gt;';
  
          echo $readyURL."\n";
      }
  }
  
  print&lt;&lt;&lt;END
                  &lt;/ul&gt;
          &lt;/nav&gt;\n
  END;
  </code></pre>
  
  <p>I have succeeded to the point where I can create the desired structure and have first  <code>&lt;li&gt;&lt;a href="/samples/samples1.html"&gt;Samples1&lt;/a&gt;&lt;/li&gt;</code> but still cannot get the rest working. Please help me </p>