[eluser]ppwalks[/eluser]
Hi all,
I was wondering if I could get some help, i am new to codeigniter and a recent graduate from university and i am taking my first plunge into the MVC world.
I recently purchased a book call "Professional Codeigniter" from the group wrox and within it is a project for a website (don't know if anyone has read if so you'll know the code probably..), the book itself hosts great opportunity to learn many of the concepts involved with making a great dynamic website but it also hosts a lot of errors which the author does tend to address on his support forum but for the last few months it seems to have stopped which led me to this forum to ask for help.
Anyway enough rambling on with the problem;
I am creating a category view for the products and I have spent 3 days trying to solve one issue which is now driving me mad. i have already fixed a numerous amount of errors but this one has got me tied up.
Model
Code:
function getCategory($id){
$data = array();
$options = array('id' =>$id);
$Q = $this->db->getwhere('categories',$options,1);
if ($Q->num_rows() > 0){
$data = $Q->row_array();
}
$Q->free_result();
return $data;
}
function getProductsByCategory($catid){
$data = array();
$this->db->select('id,name,shortdesc,thumbnail');
$this->db->where('category_id', $catid);
$this->db->where('status', 'active');
$this->db->orderby('name','asc');
$Q = $this->db->get('products');
if ($Q->num_rows() > 0) {
foreach ($Q-> result_array() as $row) {
$data[] = $row;
}
}
$Q-> free_result();
return $data;
}
Controller
Code:
function cat($id){
$cat = $this->MCats->getCategory($id);
if (!count($cat)){
redirect('welcome/index','refresh');
}
$data['title'] = "Darrens PCs | ". $cat['name'];
if ($cat['parentid'] < 1 ){
//show other categories
}else{
//show products
}
$data['category'] = $cat;
$data['main'] = 'category';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}
View
Code:
<div id='pleft'>
<?php
echo "<h2> ".$category['name']." </h2> \n";
echo "<p> ".$category['shortdesc'] . "</p>\n";
foreach ($listing as $key => $list){
echo "<img src='".$list[' border='0' align='left'/> \n";
echo "<h4>";
switch($level){
case "1":
echo anchor('welcome/cat/'.$list['id'],$list['name']);
break;
case "2":
echo anchor('welcome/product/'.$list['id'],$list['name']);
break;
}
echo "</h4> \n";
echo "<p>".$list['shortdesc']."</p><br>";
}
?>
</div>
The error generated is;
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: listing
Filename: views/category.php
Line Number: 5
AND
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/category.php
Line Number: 5