Reputation: 133
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
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