0

Codeigniter Undefined Index Online Shop:

For some reason i am getting back "Undefined index: grouping in the controller.

I have added both the controller and the model below.

I have just added the getProduct() code as well

/*Here is my model*/
  function getProductsByGroup($limit,$group,$skip){
     $data = array();
     if ($limit == 0){
        $limit=3;
     }
     $this->db->select('id,name,shortdesc,thumbnail');
     $this->db->where('grouping', $group);
     $this->db->where('status', 'active');
     $this->db->where('id !=', ($skip));
     $this->db->order_by('name','asc');
     $this->db->limit($limit);
     $Q = $this->db->get('products');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();    
    return $data; 
 }  

/*getProduct()*/
function getProduct($id){

        $data = array();
        $option = array('id' => $id);
        /*pass the id and other options to the category*/
        $Q = $this->db->get_where("categories",$option,1);
        if ($Q ->num_rows() > 0){
            $data = $Q->row_array();
        }
        $Q->free_result();
        return $data;
    }


/*This is my controller*/
    public function product($id)
    {
        $product = $this->MProducts->getProduct($id); 

        if (!count($product))
        {
        redirect('welcome/index','refresh'); 
        }

/* This is where the error is coming from*/
        $data['grouplist'] =  $this->MProducts->getProductsByGroup(3,$product['grouping'],$id);
        $data['product'] = $product;
        $data['title'] = "Claudia’s Kids | ". $product['name']; 
        $data['main'] = 'product';
        $data['navlist'] = $this->MCats->getCategoriesNav(); 
        $this->load->vars($data); 
        $this->load->view('template');
    }
user2531490
  • 17
  • 1
  • 5

3 Answers3

0

add the grouping column to the select in the getProductsByGroup function

$this->db->select('id,name,shortdesc,thumbnail, grouping');

The error is stating that $product['grouping'] is not set. If you look at your select statement you can see that you are not selecting the grouping column.

Matt
  • 2,341
  • 4
  • 20
  • 20
  • The use of `$product['grouping']` is in the call to `getProductsByGroup()`. The data is being fed from `getProducts()`. – Bad Wolf Jul 02 '13 at 19:02
  • Solved..But i must confess i love the forum : I was running my query on category table here ( $Q = $this->db->get_where("categories",$option,1);) instead of the product table. – user2531490 Jul 02 '13 at 20:06
0

You are getting the error Undefined index: grouping it occurs when you call any undefined variable or any undefined index of an array like $product['grouping']. Error states that there is no index in the $product named as the grouping so you should check the result

$product = $this->MProducts->getProduct($id); 
 var_dump($product); 

If result of var_dump shows no index like grouping then check your query

function getProduct($id){

        $data = array();
        $option = array('id' => $id);
        /*pass the id and other options to the category*/
        $Q = $this->db->query("SELECT * FROM categories WHERE id= $id");
        if ($Q ->num_rows() > 0){
            $data = $Q->row_array();
        }
        $Q->free_result();
        return $data;
    }

And make sure your category table has the column grouping

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • Thanks mate..I Have solved it now.. I was running my query on category table here ( $Q = $this->db->get_where("categories",$option,1);) instead of the product table: ( $Q = $this->db->get_where("product",$option,1);) – user2531490 Jul 02 '13 at 20:08
0

Solved..But i must confess i love the forum :

I was getting the using the category table here instead of the product table.

/*getProduct()*/
function getProduct($id){

        $data = array();
        $option = array('id' => $id);
        /*pass the id and other options to the category*/
        $Q = $this->db->get_where("categories",$option,1); i just changed this line to 
        $Q = $this->db->get_where("product",$option,1);
        if ($Q ->num_rows() > 0){
            $data = $Q->row_array();
        }
        $Q->free_result();
        return $data;
    }
user2531490
  • 17
  • 1
  • 5