Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with my recursion which retrieves categories and subcategories from my category table, my html method generates a wrong output. The subcategory is getting added twice as parent category

enter image description here

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)
share|improve this question
    
You should probably change !empty() to isset && !empty() and see how it works –  victorantunes Sep 25 '13 at 18:04
    
let me give a try with it –  fefe Sep 25 '13 at 18:10

1 Answer 1

If you select * from your #__immobilien_cat table then you will list "Luxus Villa". If you want to exclude nodes that have a parent, then you must add the clause:

select * from table where parent = 0

Then of course you will have to loop through the sub categories within each parent.

share|improve this answer
    
thansk fro feedback! in this case the subcategory won't show up –  fefe Sep 25 '13 at 17:54
    
You must loop through the subcategories one way or another. –  Sébastien Sep 25 '13 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.