Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MySQL Navigation Menu
    primarykey
    data
    text
    <p>I'm trying to create a function for a hierarchical navigational menu bar.</p> <p>I want to be able to have something like this...</p> <pre><code>&lt;ul id="navigation"&gt; &lt;li&gt;&lt;a href="#"&gt;Menu Item 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Menu Item 2&lt;/a&gt;&lt;/li&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;Sub Menu Item 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Sub Menu Item 1&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;li&gt;&lt;a href="#"&gt;Menu Item 3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Menu Item 4&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I'm using this function, but it's not working properly like I'd like it to. It's showing the main parent links, but not the child links.</p> <pre><code>function build_navbar($pid,$sub=0) { global $db; $query = $db-&gt;simple_select("navbar", "*", "pid='".$pid."'", array("order_by" =&gt; "disporder")); $menu_build = "&lt;ul id=\"navigation\"&gt;\n"; while($menu = $db-&gt;fetch_array($query)) { if($sub == 1) { $menu_build .= "&lt;ul&gt;\n"; $menu_build .= "&lt;li&gt;&lt;a href=\"#\"&gt;".$menu['title']."&lt;/a&gt;&lt;/li&gt;\n"; $menu_build .= "&lt;/ul&gt;\n"; } else { $menu_build .= "&lt;li&gt;&lt;a href=\"#\"&gt;".$menu['title']."&lt;/a&gt;&lt;/li&gt;\n"; } build_navbar($menu['id'],1); } $menu_build .= "&lt;/ul&gt;\n"; return $menu_build; } </code></pre> <p>Perhaps someone can help me fix this? Thanks.</p> <p><strong>--- New Update ---</strong></p> <p>Andy Groff, this is what your code is outputting:</p> <pre><code>&lt;ul id="navigation"&gt; &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;Child Link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Child 2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;li&gt;&lt;a href="#"&gt;Parent&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>However, I need it modified so it'll output like this:</p> <pre><code>&lt;ul id="navigation"&gt; &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;Child Link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Child 2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Parent&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p><strong>This is what it is outputting now, Andy:</strong></p> <pre><code>&lt;ul id="navigation"&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt; &lt;/li&gt; &lt;ul&gt; &lt;li&gt; &lt;a href="#"&gt;Child Link&lt;/a&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Child 2&lt;/a&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Parent&lt;/a&gt; &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload