0

我的递归有问题,它从我的类别表中检索类别和子类别,我的 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)
4

1 回答 1

0

如果您*从您的#__immobilien_cat表格中选择,那么您将列出“Luxus Villa”。如果要排除具有父节点的节点,则必须添加子句:

select * from table where parent = 0

然后,您当然必须遍历每个父级中的子类别。

于 2013-09-25T17:51:19.563 回答