Codeigniter未定义索引网店


Codeigniter Undefined Index Online Shop

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

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

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

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

您将得到错误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;
    }

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

已解决。。但我必须承认我喜欢这个论坛:

我得到的是使用类别表,而不是产品表。

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