php - Create a function to output a category hierarchy into a select drop down form input by passing the $categories array to the function -
create function output category hierarchy select drop down form input passing $categories
array function.
the given set array of data:
$categories = array( 1 => array(‘id’ => 5,‘name’ => ‘fruits’,‘sort’ => 0,‘parent’ => 0), 2 => array(‘id’ => 6, ‘name’ => ‘donuts’,‘sort’ => 1,‘parent’ => 7), 3 => array(‘id’ => 7, ‘name’ => ‘hard candy’,‘sort’ => 0,‘parent’ => 11), 4 => array(‘id’ => 8, ‘name’ => ‘pears’,‘sort’ => 3,‘parent’ => 1), 5 => array(‘id’ => 9, ‘name’ => ‘apples’,‘sort’ => 1,‘parent’ =>1), 6 => array(‘id’ => 18, ‘name’ => ‘oranges’,‘sort’ => 4,‘parent’ => 1), 7 => array(‘id’ => 19, ‘name’ => ‘sweets’,‘sort’ => 2,‘parent’ => 0), 8 => array(‘id’ => 20, ‘name’ => ‘red delicious’,‘sort’ => 5,‘parent’ => 5), 9 => array(‘id’ => 21, ‘name’ => ‘granny smith apples’,‘sort’ => 8,‘parent’ => 5), 10 => array(‘id’ => 10, ‘name’ => ‘gummy bears’,‘sort’ =>5,‘parent’ => 7), 11 => array(‘id’ => 11, ‘name’ => ‘candy’,‘sort’ => 8,‘parent’ => 7), 12 => array(‘id’ => 12, ‘name’ => ‘vegetables’,‘sort’ => 3,‘parent’ => 0), 12 => array(‘id’ => 13, ‘name’ => ‘yellow’,‘sort’ => 2,‘parent’ => 16), 14 => array(‘id’ => 14, ‘name’ => ‘grean beans’,‘sort’ => 11,‘parent’ => 12), 15 => array(‘id’ => 15, ‘name’ => ‘broccoli’,‘sort’ => 10,‘parent’ =>12), 16 => array(‘id’ => 16, ‘name’ => ‘corn’,‘sort’ => 14,‘parent’ => 12), 17 => array(‘id’ => 17, ‘name’ => ‘white’,‘sort’ => 1,‘parent’ => 16) );
note: use recursive function if can.
output should this:
<select> <option>fruits</option> <option>-apples</option> <option>--red delicious apples</option> <option>--granny smith apples</option> <option>-pears</option> <option>-organges</option> <option>sweets</option> <option>-donuts</option> <option>-gummy bears</option> <option>-candy</option> <option>--hard candy</option> <option>vegetables</option> <option>-broccoli</option> <option>-grean beans</option> <option>-corn</option> <option>--white</option> <option>--yellow</option> </select>
i got drop down of products based on category. couldn't sorting order
<?php function array_msort($array, $cols) { $colarr = array(); foreach ($cols $col => $order) { $colarr[$col] = array(); foreach ($array $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } } $eval = 'array_multisort('; foreach ($cols $col => $order) { $eval .= '$colarr[\''.$col.'\'],'.$order.','; } $eval = substr($eval,0,-1).');'; eval($eval); $ret = array(); foreach ($colarr $col => $arr) { foreach ($arr $k => $v) { $k = substr($k,1); if (!isset($ret[$k])) $ret[$k] = $array[$k]; $ret[$k][$col] = $array[$k][$col]; } } return $ret; } $categories = array( 1 => array('id' => 5,'name' => 'fruits','sort' => 0,'parent' => 0), 2 => array('id' => 6, 'name' => 'donuts','sort' => 1,'parent' => 7), 3 => array('id' => 7, 'name' => 'hard candy','sort' => 0,'parent' => 11), 4 => array('id' => 8, 'name' => 'pears','sort' => 3,'parent' => 1), 5 => array('id' => 9, 'name' => 'apples','sort' => 1,'parent' =>1), 6 => array('id' => 18, 'name' => 'oranges','sort' => 4,'parent' => 1), 7 => array('id' => 19, 'name' => 'sweets','sort' => 2,'parent' => 0), 8 => array('id' => 20, 'name' => 'red delicious','sort' => 5,'parent' => 5), 9 => array('id' => 21, 'name' => 'granny smith apples','sort' => 8,'parent' => 5), 10 => array('id' => 10, 'name' => 'gummy bears','sort' =>5,'parent' => 7), 11 => array('id' => 11, 'name' => 'candy','sort' => 8,'parent' => 7), 12 => array('id' => 12, 'name' => 'vegetables','sort' => 3,'parent' => 0), 13 => array('id' => 13, 'name' => 'yellow','sort' => 2,'parent' => 16), 14 => array('id' => 14, 'name' => 'grean beans','sort' => 11,'parent' => 12), 15 => array('id' => 15, 'name' => 'broccoli','sort' => 10,'parent' =>12), 16 => array('id' => 16, 'name' => 'corn','sort' => 14,'parent' => 12), 17 => array('id' => 17, 'name' => 'white','sort' => 1,'parent' => 16)); $arr2 = array_msort($categories, array('parent'=>sort_asc, 'sort'=>sort_asc)); for($i=count($arr2);$i>=1;$i--) { if($arr2[$i]) { $cnt=0; for($j=count($arr2);$j>=1;$j--) { if($arr2[$j] && $arr2[$j]['parent']==$i) { $arr2[$i]['child'][$cnt]['key'] = $arr2[$j]['id']; $arr2[$i]['child'][$cnt] = $arr2[$j]; $arr2[$j] = ''; $cnt++; } } } } $res = array(); $n =0; $res_arr = array_filter($arr2); for($i=1;$i<=count($arr2);$i++) { if($arr2[$i]!='') { $res[$n] = $arr2[$i]; $n++; } } $res_arr = array_filter($arr2); $str = ''; function fntest($arr, $str, $level='') { for($i=0;$i<=count($arr);$i++) { if(isset($arr[$i])) { echo '<option>'.$level.$arr[$i]['name'].'</option>'; if(isset($arr[$i]) && is_array($arr[$i]) && array_key_exists('child', $arr[$i])) { $level1 = $level.'-'; fntest($arr[$i]['child'], $str,$level1); } } } return $str; } echo '<select>'; fntest($res, $str,''); echo '</select>'; ?>
Comments
Post a Comment