Codeigniter Undefined Index在线商店

时间:2013-07-02 18:38:30

标签: php codeigniter undefined-index

Codeigniter Undefined Index在线商店:

出于某种原因,我回来了“未定义的索引:控制器中的分组。

我在下面添加了控制器和模型。

我刚刚添加了getProduct()代码

/*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');
    }

3 个答案:

答案 0 :(得分:0)

将分组列添加到getProductsByGroup函数中的select

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

该错误表明未设置$ product ['grouping']。如果查看select语句,可以看到没有选择分组列。

答案 1 :(得分:0)

当您调用任何未定义的变量或类似Undefined index: grouping的数组的未定义索引时,您会收到错误$product['grouping']。错误表明$product中没有名为grouping的索引,因此您应该检查结果

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

如果var_dump的结果没有显示像分组这样的索引,那么请检查您的查询

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;
    }

并确保您的类别表具有列分组

答案 2 :(得分:0)

解决了..但我必须承认我爱论坛:

我在这里使用类别表而不是产品表。

/*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;
    }