sanfly Posted June 23, 2009 Share Posted June 23, 2009 Hi Guys I have this function (below) which works great in its current setting, but Im now using cakePHP so I need to change it a little. I have an image gallery, there are categories in the gallery, each category can have sub categories, each sub-category can have sub-sub-categories etc, so I had this recursive function to put all the category names (in the correct order) in a select box so when I was uploading or moving an image I could select the correct destination category The function below just basically spits out (echos) each line as it comes to it - this was fine for my old site because I would just call the function in the spot where I wanted the select box to appear. NOW I want to collect up all the outputs into a string, then echo them in a place of my choosing. Any ideas how I do this? The Current Function <?function display_children($parent, $level, $selectedParent) { // retrieve all children of $parent include "includes/db.php"; $r = mysql_query("SELECT * FROM gal_cat WHERE gc_parent = '$parent' ORDER BY gc_order") or die(mysql_error()); // display each child while ($row = mysql_fetch_array($r)) { $gc_id = $row['gc_id']; $gc_name = $row['gc_name']; $gc_level = $row['gc_level']; $indent = str_repeat(' ',$level) // indent and display the title of this child ?> <option value="<?=$gc_id?>" <? if($gc_id == $selectedParent){ echo "selected"; } ?> class="optionHeight"><?=$indent?><?=$gc_name?><br> <? // call this function again to display this // child's children display_children($gc_id, $level+1, $selectedParent); } } ?> Link to comment https://forums.phpfreaks.com/topic/163412-recursivereiterative-function/ Share on other sites More sharing options...
flyhoney Posted June 23, 2009 Share Posted June 23, 2009 Untested, but something like this might work: <?php function display_children($parent, $level, $selectedParent) { // retrieve all children of $parent include "includes/db.php"; $r = mysql_query("SELECT * FROM gal_cat WHERE gc_parent = '$parent' ORDER BY gc_order") or die(mysql_error()); while ($row = mysql_fetch_array($r)) { $gc_id = $row['gc_id']; $gc_name = $row['gc_name']; $gc_level = $row['gc_level']; $indent = str_repeat(' ',$level); $html = '<option value="'.$gc_id.'" '.($gc_id == $selectedParent ? "selected" : '').' class="optionHeight">'.$indent.$gc_name.'<br>'; return $html . display_children($gc_id, $level + 1, $selectedParent); } } ?> Link to comment https://forums.phpfreaks.com/topic/163412-recursivereiterative-function/#findComment-862183 Share on other sites More sharing options...
sanfly Posted June 23, 2009 Author Share Posted June 23, 2009 Good idea, but unfortunately didnt work - it only went down one branch eg: If my "dir" structure is: 2008 Winter Games Season Parties 2009 Ski Sale Winter Games All that was returned was: 2008 Winter Games Season Parties Link to comment https://forums.phpfreaks.com/topic/163412-recursivereiterative-function/#findComment-862253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.