No assumption was made, I was merely providing some information that seemed relevant to using 0 or NULL as a parent. I have no idea what the actual table looks like considering the user never posted the create table statement. For all I know every column could be part of a unique key. Also, the $runner is there to identify the depth, which in most cases is a useful “hook” for CSS to have.
By the way… thanks for pointing out that gigantic inefficiency issue – much appreciated.
The reason it was there though is because I pulled that function from this class where it makes more sense.
<?php
namespace UI\\Element\\Common\\Listing;
class Tree implements \\UI\\Element {
public function settings() {
return array(
'data'=>array(
'default'=>array()
)
,'child_key'=>array(
'default'=>'children'
)
,'value_key'=>array(
'default'=>'value'
)
,'list_element'=>array(
'default'=>'ul'
)
,'depth_class'=>array(
'default'=>true
)
,'mutation'=>array(
'default'=>null
)
,'form'=>array( // flag to wrap contents in form element
'default'=>false
)
,'form_action'=>array( // form action, when form
'default'=>''
)
,'form_name'=>array( // form name, when form
'default'=>''
)
,'form_method'=>array( // form action, when form
'default'=>''
)
,'form_legend'=>array( // form legend, when form
'default'=>''
)
);
}
public function html($settings,\\UI\\Manager $ui) {
return $this->_html($settings);
}
private function _html($settings,$runner=0) {
extract($settings);
$out = '';
if($form === true) {
$out.= sprintf(
'<form name="%s" action="%s" method="%s"><fieldset><legend>%s</legend>'
,$form_name
,$form_action
,$form_method
,$form_legend
);
}
/*
* Build out tree
*/
if(!empty($data)) {
$out.="<$list_element>";
foreach($data as $index=>$item) {
$out.= sprintf(
'<li%s>%s%s</li>'
,$depth_class === true?' class="depth-'.$runner.'"':''
,$mutation !== null?call_user_func_array($mutation,array($item[$value_key],$item,$index)):$item[$value_key]
,$this->_html(array(
'data'=>isset($item[$child_key]) && !empty($item[$child_key])?$item[$child_key]:array()
,'child_key'=>$child_key
,'value_key'=>$value_key
,'list_element'=>$list_element
,'depth_class'=>$depth_class
,'mutation'=>$mutation
),($runner+1))
);
}
$out.="</$list_element>";
}
if($form === true) {
$out.= '</fieldset></form>';
}
return $out;
}
}
?>
By which I use to build out trees.
echo $this->ui('Common.Listing.Tree',array(
'data'=>$terms
,'value_key'=>'human_name'
,'child_key'=>'terms'
,'list_element'=>'ul'
,'mutation'=>$mutation
,'form'=>true
,'form_legend'=>$vocabulary?$vocabulary['human_name']:''
,'form_action'=>$frm_action
,'form_method'=>$frm_method
,'form_name'=>$frm_name
));