未定义的偏移量:Codeigniter中的0

时间:2016-01-21 10:07:24

标签: php codeigniter

当我发出请求此页面时,我收到了类似的错误

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: models/mproduk.php

Line Number: 116

这是我的控制器

public function update($id=''){
        $data = array(); 
        $data['size'] = $this->msize->tampil();

        $data['eta']=$this->mproduk->berapakah_stoknya($id);
        $data['cats']   = $this->mkategori->tampil();
        $data['prd']    = $this->mproduk->get_produk_detail($id);
        $data['human']    = $this->mhuman->tampil();

        $html = array();
        $html['header']     = $this->load->view('secuser/header',null,true);
        $html['kiri']       = $this->load->view('secuser/kiri',null,true);
        $html['content']    = $this->load->view('secuser/product/update',$data,true);
        $this->load->view('secuser/template',$html);
    }

这是我的模特

public function berapakah_stoknya($kodepro){  
        $this->load->model('msize');
        $hahaha=$this->msize->tampil();

        foreach($hahaha as $wkwkwkw){
            $apa= $wkwkwkw['id'];
            $sql= "select  ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
            $ggg= $this->db->query($sql)->result_array();    
            $return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
        }
        return $return;
    }

如果我使用 error_reporting(0),页面将循环并显示为某个页面

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

您的数据库查询检索不到任何结果,因此$ggg为空且没有0索引。

更改模型以检查是否找到任何结果。如果找到,请添加到$return数组。

public function berapakah_stoknya($kodepro){  
    $this->load->model('msize');
    $hahaha = $this->msize->tampil();
    $return = array();
    foreach($hahaha as $wkwkwkw){
        $apa= $wkwkwkw['id'];
        $sql= "select  ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
        $ggg= $this->db->query($sql)->result_array();

        if(count($ggg)>0){
          $return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
        }
    }
    return $return;
}

您似乎期望只检索一行。所以你可以使用row_array

public function berapakah_stoknya($kodepro){  
    $this->load->model('msize');
    $hahaha = $this->msize->tampil();
    $return = array();
    foreach($hahaha as $wkwkwkw){
        $apa= $wkwkwkw['id'];
        $sql= "select  ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
        $query = $this->db->query($sql);

        if($query->num_rows() > 0){
          $ggg = $query->row_array();
          $return[]=array('stok'=>$ggg["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
        }
    }
    return $return;
}