fraction Posted July 28, 2008 Share Posted July 28, 2008 I'm trying to create a hierarchical drop-down list from an array i've created. Totally stumped. Here's a piece of the sample array: [0] => Array ( [id] => 19 [name] => Food [parentid] => 1 [type] => 1 ) [1] => Array ( [id] => 32 [name] => Apple [parentid] => 19 [type] => 1 ) [2] => Array ( [id] => 33 [name] => Orange [parentid] => 19 [type] => 2 ) I'd like to get to this point: <option value="19">Food</option> <option value="32"> - Apple</option> <option value="33"> - Orange</option> Basically, creating a list like: Parent1 -Child1 -Child2 --Subchild1 Parent2 etc. Any help would be greatly appreciated. Cheers. Jon Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/ Share on other sites More sharing options...
Barand Posted July 29, 2008 Share Posted July 29, 2008 What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602169 Share on other sites More sharing options...
fraction Posted July 29, 2008 Author Share Posted July 29, 2008 Honestly? nothing. I simply don't know what path I should follow. This is more of a logic question than a coding question. Should I rearrange the array? Some sort of recursive function? <shrug /> Stumped. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602199 Share on other sites More sharing options...
Barand Posted July 29, 2008 Share Posted July 29, 2008 Yes you need a recursive function <?php function printOptions ($options, $parent, $level=0) { foreach ($options as $opt) { if ($parent == $opt['parentid']) { $indent = str_repeat('---', $level); echo "<option value='{$opt['id']}'>$indent {$opt['name']}</option>\n"; printOptions($options, $opt['id'], $level+1); } } } $options = array ( Array ( 'id' => 19, 'name' => 'Food', 'parentid' => 1, 'type' => 1 ), Array ( 'id' => 32, 'name' => 'Apple', 'parentid' => 19, 'type' => 1 ), Array ( 'id' => 33, 'name' => 'Orange', 'parentid' => 19, 'type' => 2 ) ); echo "<select name='myselect'>\n" ; // call function printOptions ($options,1); echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602204 Share on other sites More sharing options...
fraction Posted July 29, 2008 Author Share Posted July 29, 2008 Ok, I see what you're getting at. Trick here is, you're setting the parent in the function call. I need to make that dynamic. So we create everything for parent 1, then move on to the next parent in the array. Example Parent 1 - child 1 - child 2 -- sub-child 1 -- sub-child 2 Parent 2 - child 1 etc. In another recursive function I built, i continued to call the same function until we reached a leaf on the tree, then moved on to the next parent. Any chance that could work here? Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602260 Share on other sites More sharing options...
Barand Posted July 29, 2008 Share Posted July 29, 2008 Normally you'd start with root items (those with no parent ie parent id==0) but you didn't have any of those in your array, so I started at 1. Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602461 Share on other sites More sharing options...
fraction Posted July 29, 2008 Author Share Posted July 29, 2008 Much thanks, Barand. I gotta keep that piece of code around for next time. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/117073-solved-create-hierarchical-drop-down-list-from-array/#findComment-602746 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.