我的递归问题从类别表中检索类别和子类别,我的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)
答案 0 :(得分:0)
如果您从*
表中选择#__immobilien_cat
,则会列出“Luxus Villa”。如果要排除具有父节点的节点,则必须添加以下子句:
select * from table where parent = 0
当然,你必须遍历每个父母中的子类别。