Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate menu from multi dimensional infinite parent child tree
    primarykey
    data
    text
    <p>Anyone?</p> <p>Thanks in advance, and let me know if you need any additional info.</p> <p>I am attempting to turn a multi dimensional array to a multiple nested html navigation menu. I have the jist of it from another answer on SO: <a href="https://stackoverflow.com/questions/8685825/php-loop-through-multidimensional-array-and-establish-parent-child-relationship">solution here</a>.</p> <p>What I am trying to figure out is how to preserve the top level parent link (and any subsequent child page links) in the url for the next child array. I tried passing in the link to the function when it calls itself to build the array, but that only preserved the most recent parent link.</p> <p>Example:</p> <p>Home About<br> -Info<br> --Sub Page</p> <p>becomes:</p> <p>home about about/info about/info/subpage</p> <p>Here is a sample array:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [id] =&gt; 12 [parent] =&gt; 11 [name] =&gt; Sub Page [link] =&gt; sub_page [target] =&gt; _self ) [1] =&gt; stdClass Object ( [id] =&gt; 14 [parent] =&gt; 12 [name] =&gt; Test [link] =&gt; test_test [target] =&gt; _self ) [2] =&gt; stdClass Object ( [id] =&gt; 9 [parent] =&gt; 0 [name] =&gt; Home [link] =&gt; home [target] =&gt; _self ) [3] =&gt; stdClass Object ( [id] =&gt; 11 [parent] =&gt; 10 [name] =&gt; Info [link] =&gt; info [target] =&gt; _self ) [4] =&gt; stdClass Object ( [id] =&gt; 13 [parent] =&gt; 10 [name] =&gt; Test [link] =&gt; test [target] =&gt; _self ) [5] =&gt; stdClass Object ( [id] =&gt; 10 [parent] =&gt; 0 [name] =&gt; About [link] =&gt; about [target] =&gt; _self ) </code></pre> <p>)</p> <p>And here is the code I am using:</p> <pre><code>function create_menu_array($arr, $parent = 0){ $pages = array(); foreach($arr as $page){ if($page-&gt;parent == $parent){ $page-&gt;sub = isset($page-&gt;sub) ? $page-&gt;sub : $this-&gt;create_menu_array($arr, $page-&gt;id); $pages[] = $page; } } return $pages; } function create_menu_html($nav){ $html = ''; foreach($nav as $page){ $html .= '&lt;ul&gt;&lt;li&gt;'; $html .= '&lt;a href="' . base_url().$page-&gt;link . '" target="'.$page-&gt;target.'"&gt;' . $page-&gt;name . '&lt;/a&gt;'; $html .= $this-&gt;create_menu_html($page-&gt;sub); $html .= '&lt;/li&gt;&lt;/ul&gt;'; } return $html; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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