Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you need to generate something called a super tree and use a recursive function to echo out each branch, this will allow for an unlimited depth tree, but, it also allows for an unknown depth tree.</p> <p><strong>First, SQL:</strong></p> <pre><code>SELECT category_id, parent, category_name </code></pre> <p><strong>Second, use SQL to create tree:</strong></p> <pre><code>$super_tree = array(); while(($row = mysql_fetch_assoc($res)) != NULL) { $parent = $row['parent'] == NULL ? 0 : $row['parent']; //clean up the parent $super_tree[$parent][$row['category_id']] = $row['category_name']; } </code></pre> <p><strong>Third, echo out the tree</strong></p> <pre><code>function echo_branch($tree_branch, $tree_root) { echo("&lt;ul&gt;\n"); //open up our list for this branch foreach($tree_branch as $id =&gt; $name) { //foreach leaf on the branch echo("&lt;li&gt;"); //create a list item echo("&lt;a href=\"#{$id}\"&gt;{$name}&lt;/a&gt;"); //echo out our link if(!empty($tree_root[$id])) { //if our branch has any sub branches, echo those now echo_branch($tree_root[$id], $tree_root); //pass the new branch, plus the root } echo("&lt;/li&gt;\n"); //close off this item } echo("&lt;/ul&gt;\n"); //close off this list } echo_branch($super_tree[0], $super_tree); //echo out unlimited depth tree structure </code></pre> <p>this allows for unlimited depth in your tree structure and allows for simple code to be able to create the structure within the code base.</p> <p>All that you would need to do now is add in your extra's such as classes and your extra html elements in the correct places.</p> <p>if you are looking to track the current depth of the tree to be able to echo different things depending on the depth, you can make the following alterations</p> <p><strong>In the function definition</strong></p> <pre><code>function echo_branch($tree_branch, $tree_root, $depth) { </code></pre> <p><strong>In the recursive call within the if</strong></p> <pre><code>echo_branch($tree_root[$id], $tree_root, $depth++); </code></pre> <p><strong>In the initial call</strong></p> <pre><code>echo_branch($super_tree[0], $super_tree, 0); </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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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