我的递归有问题,它从我的类别表中检索类别和子类别,我的 html 方法生成错误的输出。子类别被添加为父类别两次
public function getCategories() {
$refs = array();
$list = array();
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__immobilien_cat');
$db->setQuery($query);
$datas = $db->loadAssocList();
$categories = array();
foreach ($datas as $data) {
$thisref = &$refs[$data['id']];
$thisref['parent'] = $data['parent'];
$thisref['cat_name'] = $data['cat_name'];
$thisref['id'] = $data['id'];
if ($data['parent'] == 0) {
$list[$data['id']] = &$thisref;
} else {
$refs[$data['parent']]['children'][$data['id']] = &$thisref;
}
}
return $this->toHTMl($refs);
}
function toHTML(array $array) {
$html = '<ol class="dd-list">' ;
foreach ($array as $value) {
$html .= '<li data-id="'.$value['id'].'" class="dd-item dd3-item">';
$html .= '<div class="dd-handle dd3-handle"></div>';
$html .= '<div class="dd3-content">'.$value['cat_name'].'</div>';
if (!empty($value['children'])) {
$html .= $this->toUL($value['children']);
}
$html .= '</li>';
}
$html .= '</ol >' ;
return $html;
}
id cat_name parent translation status
------ ----------- ------ ----------- --------
1 Villa 0 (NULL) (NULL)
2 Büro 0 (NULL) (NULL)
3 Apartment 0 (NULL) (NULL)
4 Luxus Villa 1 (NULL) (NULL)