tryingtolearn Posted May 17, 2012 Share Posted May 17, 2012 Been using a function I found here a while back for listing categories and sub categories and it works perfect. function listSubcats ($parent, $level=0){ global $abc; $sql = "SELECT id, title FROM Cat WHERE parent = $parent"; $res = mysqli_query($abc, $sql); while (list($id, $title) = $res->fetch_row()) { $indent = str_repeat('-', $level); echo "<OPTION value='$id'>$indent $title</OPTION>\n"; listSubcats ($id, $level+1); // list its subcats } } But this is the first time I need change the way the results are displayed. And that leads me to realizing I am a bit confused on how list really works. The mysql table has the typical ID, CatName, parent I need to have each Cat with all subcats related to it listed in its own div. But whatever I try I cannot get the placements of the opening and closing divs in the right spots. So can anyone tell if its even possible to do it with this function. I did work it out using multiple queries but then read on alot of forums that queries inside while statements is not good. Just looking for he best (Correct) way of geting the results laid out properly. like this <div class="one-third column"> Cat1 </div> <div class="one-third column"> Cat 2 </div> <div class="one-third column"> Cat 3 - Subcat 1 - Subcat 2 -- Sub Subcat 1 </div> <div class="one-third column"> Cat 4 </div> Hope that makes sense... Thanks for any guidance. Quote Link to comment https://forums.phpfreaks.com/topic/262704-help-with-using-list-and-arranging-mysql-results/ Share on other sites More sharing options...
Barand Posted May 17, 2012 Share Posted May 17, 2012 try function listSubcats ($parent, $level=0){ global $abc; $sql = "SELECT id, title FROM Cat WHERE parent = $parent"; $res = mysqli_query($abc, $sql); while (list($id, $title) = $res->fetch_row()) { if ($level==0) echo "<div class='one-third column'>\n"; $indent = str_repeat('-', $level); echo "$indent$title<br />\n"; listSubcats ($id, $level+1); // list its subcats if ($level==0) echo "</div>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/262704-help-with-using-list-and-arranging-mysql-results/#findComment-1346474 Share on other sites More sharing options...
tryingtolearn Posted May 18, 2012 Author Share Posted May 18, 2012 Works perfect, I wasnt adding anything in the second cll to the function but now that I see it it makes sense!! Thanks for taking the time. I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/262704-help-with-using-list-and-arranging-mysql-results/#findComment-1346534 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.