Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem to generate nested ul lists using PHP
    primarykey
    data
    text
    <p>I am working on a front-end web app where a nested unordered list would be used for the jQuery plugin mcdropdown.</p> <p>Here is the data structure from PHP: a nested array of arrays :</p> <pre><code>Array ( [0] =&gt; Array ( [fullpath] =&gt; ../foil/alphanumeric/ [depth] =&gt; 0 ) [1] =&gt; Array ( [fullpath] =&gt; ../foil/alphanumeric/letters/ [depth] =&gt; 1 ) [2] =&gt; Array ( [fullpath] =&gt; ../foil/alphanumeric/numbers/ [depth] =&gt; 1 ) [3] =&gt; Array ( [fullpath] =&gt; ../foil/alphanumeric/numbers/symbols/ [depth] =&gt; 2 ) ) </code></pre> <p>Basically, I took the excellent answer from <a href="https://stackoverflow.com/questions/1310649/getting-a-modified-preorder-tree-traversal-model-nested-set-into-a-ul">this question on SO</a>, modified it a bit :</p> <pre><code>global $fullpaths; // $fullpaths contains the above data structure in print_r $result = ''; $currentDepth = -1; while(!empty($fullpaths)) { $currentNode = array_shift($fullpaths); if($currentNode['depth'] &gt; $currentDepth) { $result .='&lt;ul&gt;'; } if($currentNode['depth'] &lt; $currentDepth) { $result .=str_repeat('&lt;/ul&gt;', $currentDepth - $currentNode['depth']); } $result .= '&lt;li&gt;'. $currentNode['fullpath'] .'&lt;/li&gt;'; $currentDepth = $currentNode['depth']; if(empty($fullpaths)) { $result .= str_repeat('&lt;/ul&gt;', 1 + $currentDepth); } } print $result; </code></pre> <p>and got the following output:</p> <pre><code>&lt;ul&gt; &lt;li&gt;../foil/alphanumeric/&lt;/li&gt; &lt;ul&gt; &lt;li&gt;../foil/alphanumeric/letters/&lt;/li&gt; &lt;li&gt;../foil/alphanumeric/numbers/&lt;/li&gt; &lt;ul&gt; &lt;li&gt;../foil/alphanumeric/numbers/symbols/&lt;/li&gt; &lt;/ul&gt; &lt;/ul&gt; &lt;/ul&gt; </code></pre> <p>Which cannot be accepted by the mcdropdown jQuery plugin, it expects something like this:</p> <pre><code>&lt;li rel="1"&gt; 'Alphanumeric' &lt;ul&gt; &lt;li rel="2"&gt;'Letters'&lt;/li&gt; &lt;li rel="3"&gt;'Numbers' &lt;ul&gt; &lt;li rel="4"&gt;'Symbols'&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; </code></pre> <p>To be frank, I don't quite understand how the answer from that question works, I have been trying to modify that solution to cope with my situation, but still failed.</p> <p>Any help and suggestion is much appropriated in advance.</p>
    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