0

Codeigniter 未定义索引在线商店:

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

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

我也刚刚添加了 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');
    }
4

3 回答 3

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;
    }
于 2013-07-03T06:18:25.773 回答
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;
    }

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

于 2013-07-02T19:40:44.277 回答
0

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

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

错误说明 $product['grouping'] 未设置。如果您查看您的选择语句,您会发现您没有选择分组列。

于 2013-07-02T18:56:36.590 回答