【问题标题】:Codeigniter Undefined Index Online ShopCodeigniter 未定义索引在线商店
【发布时间】:2013-06-30 06:36:42
【问题描述】:

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

【问题讨论】:

  • 在您的问题中发布getProduct()的代码
  • 我刚刚做到了。谢谢
  • 做一个 print_r($product);死();在您设置 $product 变量之后...查看 $product['grouping'] 是否为空。错误表明 $product['grouping'] 丢失
  • 我在这段代码 ($product = $this->MProducts->getProduct($id);) 之后做了一个 foreach 语句,它看起来很好,我可以获得产品 id...

标签: php codeigniter undefined-index


【解决方案1】:

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

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

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

【讨论】:

  • $product['grouping'] 的使用是在对getProductsByGroup() 的调用中。数据来自getProducts()
  • 已解决..但我必须承认我喜欢这个论坛:我在这里的类别表上运行我的查询( $Q = $this->db->get_where("categories",$option,1 );) 而不是产品表。
【解决方案2】:

当您调用任何未定义的变量或数组的任何未定义索引(如$product['grouping'])时,您会收到错误Undefined index: 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;
    }

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

【讨论】:

  • 谢谢队友..我现在已经解决了..我在这里的类别表上运行我的查询( $Q = $this->db->get_where("categories",$option,1) ;) 而不是产品表:( $Q = $this->db->get_where("product",$option,1);)
【解决方案3】:

解决了..但我必须承认我喜欢这个论坛:

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

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

【讨论】:

    猜你喜欢
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多