Siva Shanker
Siva Shanker

Reputation: 133

Move the category list into the products view

This is the sample controller

public function products()
    {
           $category['catlist']=$this->usermodel->catlist();
           $this->load->view('admin/products',$category);

    }

this is the model

public function catlist()
    {
        $query=$this->db->get('category');
        if($query->num_rows()>0)
        {
            return $query->result();
        }
        else
        {
            return null;
        }
    }

This is the view

 <select name="categoryname">
                    <?php 
                    foreach($catlist as $categorylist)
                    {
                    ?>
                    <option><?php echo $categorylist->categoryname;?> </option>
                    <?php 
                    }
                    ?>
                </select>

I want to display the categorylist but getting the error like

Message: Undefined variable: catlist

Message: Invalid argument supplied for foreach()

Upvotes: 0

Views: 216

Answers (1)

Ghostman
Ghostman

Reputation: 6114

if (is_array($catlist))
{
    foreach ($catlist  as $categorylist){
        ...
    }
}

The reasons is so you're not allocating an empty array when you've got nothing to begin with anyway.

Upvotes: 1

Related Questions