son Posted March 20, 2012 Share Posted March 20, 2012 I got the following the display categories in two levels as: // get the top level halos first$haloQuery = 'SELECT haloID, halo FROM haloTable WHERE haloParent = 0';$haloResult = mysqli_query($dbc, $haloQuery);if (mysqli_num_rows($haloResult) > 0){ while ($row = mysqli_fetch_array ($haloResult, MYSQL_ASSOC)) { $haloID = $row['haloID']; $halo = $row['halo']; echo "<option value=\"halos.php?hid={$haloID}\">" . $halo . "</option>\n"; // get the sub halos now $haloQuery2 = "SELECT haloID, halo FROM haloTable WHERE haloParent = $haloID"; $haloResult2 = mysqli_query ($dbc, $haloQuery2); if (mysqli_num_rows($haloResult2) > 0) { while ($row = mysqli_fetch_array ($haloResult2, MYSQL_ASSOC)) { $haloSubID = $row['haloID']; $haloSub = $row['halo']; echo "<option value=\"halos.php?hid={$haloSubID}\" style=\"padding-left:10px;\">" . $halo . "</option>\n"; } } }} Now, where I cannot get my head around: if I was to allow that the not only the top level halos could be choosen as parent, but also all other halos who can I loop this somehow that it will go as deep as needed to display "top level halos, level 2 halos relevant to top level halo just listed, level 3 halos relevant to level 2 halo just listed etc... I hope you know what I mean... Son Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 You need to use a recursive function, which is a function that calls itself. You would pass the function the ID of the parent and it would display all of the children and call itself for every child to display the children under that. You would start it off by passing a parent ID of 0 to list all of the top-level items first. Link to comment Share on other sites More sharing options...
son Posted March 20, 2012 Author Share Posted March 20, 2012 I cannot get this working. The problem lies aleady in establishing the array data. I have now: $rq = 'SELECT haloID, halo, haloParent FROM haloTable';$rs = mysqli_query($dbc, $rq); $childrenTree = array(); //store an array of children for each parent $categoryNames = array(); //store category name for each id //fill $childrenTree and $categoryNames from database while (list($haloID, $halo, $haloParent) = mysqli_fetch_array($rs, MYSQL_ASSOC)) { $categoryNames[(string)$haloID] = $halo; $haloParent = (string)$haloParent; if(!array_key_exists($haloParent, $childrenTree)) $childrenTree[$haloParent] = array(); $childrenTree[$haloParent][] = (string)$haloID; }function renderTree($parent = "0"){ global $categoryNames; global $childrenTree; if($parent != "0") echo "<li> ", $categoryNames[$parent], "\n"; $children = $childrenTree[$parent]; if(count($children) > 0){ //If node has children echo "<ul>\n"; foreach($children as $child) renderTree($child); echo "</ul>\n"; } if($parent != "0") echo "</li>\n";}renderTree(); //This renders the hierarchical tree Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 What is the result? Are the arrays structured like you meant them to be? Link to comment Share on other sites More sharing options...
son Posted March 21, 2012 Author Share Posted March 21, 2012 No result whatsoever, empty screen. I run the query in phpMyAdmin to make sure there is no issue and it was running in there ok... Son Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 If you print the arrays do they contain the data you expect? If you add output to the start of the function does it get called several times? Link to comment Share on other sites More sharing options...
son Posted March 21, 2012 Author Share Posted March 21, 2012 It did not get any data. Found the issue. The data from the three columns were mixed type, but I used mysqli_fetch_array($rs, MYSQL_ASSOC) I did replace this now with mysqli_fetch_array($rs, MYSQLI_BOTH) which gets the data fine and the tree is displayed. Now I have to figure out how to change the whole thing to a select drop down as tree much longer than I thought... Many thanks for pointers,Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.