Welcome Guest, Not a member yet? Register   Sign In
Multi level category menu problem
#1

[eluser]sihijau[/eluser]
Hi i create multi level category menu from database, category table structure:
-id
-parentid
-name
-url

i create methods in category model
method 1
Code:
function getActiveCategory(){
        //$this->db->where('status','active');
        $this->db->orderby('parentid','asc')->groupby('parentid,id');
        $q=$this->db->get('categories');
        foreach($q->result() as $r){
            $data[$r->parentid][] = $r;
        }
        $menu=$this->getCategoryMenu($data,0);
        return $menu;
    }
method 2
Code:
function getCategoryMenu($category,$parent){
        static $i = 1;
        if ($category[$parent]) {
            $menu = '<ul>';
            $i++;
            foreach ($category[$parent] as $r) {
                $child = $this->getCategoryMenu($category, $r->id);
                $menu .= '<li id="'.$parent.'">';
                $menu .= '<a href="#">'.$r->name.'</a>';
                if ($child) {
                    $i--;
                    $menu .= $child;
                }
                $menu .= '</li>';
            }
            $menu .= '</ul>';
            return $menu;
        } else {
            return false;
        }
    }

when i call getActiveCategory on my controller method works, it show me list

* CLOTHES
o Shoes
+ Sport Shoes
o T-shirts
o pants
o dresses
o test2
* FUN
o toys
o games
+ PC Games
+ PS2 Games
* OTHER

but i got an error:

Quote:Severity: Notice

Message: Undefined index: 12

Filename: models/mcategory.php

Line Number: 61

Line 61 on my IDE point to
Quote:if ($category[$parent]) {

Could anyone help me please..?
#2

[eluser]dmorin[/eluser]
Code:
if ($category[$parent])
What are you trying to accomplish here? Is that checking to make sure that the index exists? If so, use:
Code:
if (isset($category[$parent]))
or
Code:
if (array_key_exists($parent, $category))
#3

[eluser]sihijau[/eluser]
Oh thanks for pointing me to check array key Smile using array_key_exists()
problem solved




Theme © iAndrew 2016 - Forum software by © MyBB