digitalgod Posted August 14, 2008 Share Posted August 14, 2008 hey guys, I'm trying to build a recursive function to display all of my categories and subcategories but I keep running into some logic problems... what I basically need it to do is echo something like this <li><span class="folder">Folder 1</span></li> <li><span class="folder">Folder 2</span> <ul> <li><span class="folder">Subfolder 2.1</span> <ul id="folder21"> <li><span class="folder">Subfolder 2.1.1</span></li> </ul> </li> <li><span class="folder">Subfolder 2.2</span></li> </ul> </li> so main folder that don't have any children are between <li></li> tags, folders that do have children have <ul></ul> before the closing </li> I can't figure this out and I know it shouldn't be that hard but it's getting late and I'm extremely tired... this is what I have right now and it's no where near what I want it to do function recursiveTree($parent, $level) { $result = mysql_query('SELECT * FROM image_category '. 'WHERE parent_id="'.$parent.'";'); $result_count = mysql_num_rows($result); // display each child while ($row = mysql_fetch_array($result)) { $new_level = $level + 1; if ( $new_level == 1 ) { echo '<li><span class="folder">'.$row['title'].'<ul>'; } else { echo '<li><span class="folder">'.$row['title']; if ( $result_count == 1 || $result_count == $new_level ) { echo '<ul>'; } else { echo '</li></ul>'; } } recursiveTree($row['image_category_id'], $level+1); } } Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/ Share on other sites More sharing options...
ignace Posted August 14, 2008 Share Posted August 14, 2008 function recursiveTree($parent) { $result = mysql_query('SELECT * FROM image_category '. 'WHERE parent_id="'.$parent.'";'); $result_count = mysql_num_rows($result); // display each child echo '<ul>'; while ($row = mysql_fetch_array($result)) { if ($parent !== $row['image_category_id']) { // or something else that changes and means that we are going a level deeper recursiveTree($row['image_category_id']); } else { echo '<li><span class="folder">' . $row['title'] . '</span></li>'; } } echo '</ul>'; } post your table fields Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-616304 Share on other sites More sharing options...
digitalgod Posted August 14, 2008 Author Share Posted August 14, 2008 hey ignace, that actually won't echo out anything because main folders have no parent_id so they're parent_id value is 0 if that makes any sense... That's why I had $level, to keep track of how deep it goes in one branch that table image_category has image_category_id | parent_id | title | description | created Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-616314 Share on other sites More sharing options...
digitalgod Posted August 14, 2008 Author Share Posted August 14, 2008 anyone? I'm really going crazy trying to get this function to work Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-616960 Share on other sites More sharing options...
akitchin Posted August 14, 2008 Share Posted August 14, 2008 it's a pretty simple matter, but it can be tricky to nail down. the solution is to wrap a <ul> around the entire list if it is a child of something, since all children have parents: <?php function recursiveTree($parent = 0) { // if this is a child, add a set of <ul> tags if ($parent > 0) { echo '<ul>'; } $result = mysql_query('SELECT * FROM image_category '. 'WHERE parent_id="'.$parent.'";'); // display each child while ($row = mysql_fetch_array($result)) { echo '<li><span class="folder">'.$row['title'].'</span>'; recursiveTree($row['category_id']); echo '</li>'; } // if this is a child, finish off the <ul> tag if ($parent > 0) { echo '</ul>'; } } ?> give that a whirl. to be honest, i'm not incredibly experienced with recursives so i can't promise that will solve your problem. worth a shot though. Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-616968 Share on other sites More sharing options...
digitalgod Posted August 14, 2008 Author Share Posted August 14, 2008 thanks akitchin! I've always hated recursives... my only problem though is that it creates unnecessary markup ( I'm a neat freak as well as a web standards freak hehe) but I've lost so much time trying to get those damn <ul> tags to close at the right place that I don't really mind. One question though, is there a way to keep the generated markup nice and clean, because right now it prints everything on one line and adding \n isn't helping... Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-616975 Share on other sites More sharing options...
akitchin Posted August 14, 2008 Share Posted August 14, 2008 try adding chr(10) and chr(13) whenever you want a linebreak (and a tab character to move it across). those two together are a newline and a return carriage. some browsers need both to render the line break. as for unnecessary markup, is that not what you wanted in terms of the <ul></ul> tags? it (should) generate one set for each child, regardless of level. is that not what it is spitting out? Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-617000 Share on other sites More sharing options...
digitalgod Posted August 15, 2008 Author Share Posted August 15, 2008 thanks for the tip! Here I'll show you what I mean by unnecessary markup: <ul id="browser" class="filetree"> <li><span class="folder">main</span> <ul> <li><span class="folder">sub-main</span> <ul> <!-- unnecessary --> </ul> <!-- unnecessary --> </li> </ul> </li> <li><span class="folder">main #2</span> <ul> <li><span class="folder">test #2</span> <ul> <li><span class="folder">test #4</span> <ul> <li><span class="folder">test #5</span> <ul> <!-- unnecessary --> </ul> <!-- unnecessary --> </li> </ul> </li> </ul> </li> <li><span class="folder">test #3</span> <ul> <!-- unnecessary --> </ul> <!-- unnecessary --> </li> </ul> </li> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-617024 Share on other sites More sharing options...
akitchin Posted August 15, 2008 Share Posted August 15, 2008 my bad - move the if() statement to below the query (but before the while loop) and check mysql_num_rows() on the resource to verify that it's got more than 0. if it fails on either parent > 0 or num_rows > 0, then it shouldn't display <ul> (nor </ul>). Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-617074 Share on other sites More sharing options...
digitalgod Posted August 16, 2008 Author Share Posted August 16, 2008 it still doesn't work, I'll try to figure it out later on, I'll put it on my to do list. thanks for your help akitchin! Quote Link to comment https://forums.phpfreaks.com/topic/119622-solved-help-with-recursive-function/#findComment-618093 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.