Codeigniter:致命错误:无法使用类型为CI_DB_mysql_result的对象作为数组

时间:2015-04-13 04:48:19

标签: php codeigniter

当数组值从控制器传递到视图页面时出现错误。

我的代码如下:

$a[$month] = $ii;   //Getting an Array values
$data['values']=$a;  //Passing array values to view page as declared as $data

我的控制器功能:

function reports()
{
    if($this->input->post('getit'))
    {
        $pro_id=$this->input->post('product').'<br>';       
        $fdate= date('d', strtotime($this->input->post('from')));
        $fmonth = date('m', strtotime($this->input->post('from')));
        $tdate = date('d', strtotime($this->input->post('to')));
        $tmonth = date('m', strtotime($this->input->post('to')));

        $year= $this->input->post('year');

        $data = $this->model_select->date_range($fdate,$fmonth,$tdate,$tmonth,$year); 

        $ii=0;
        foreach ($data->result() as $dat){
            $dat->order_id;$month=date('m', strtotime($dat->order_date));

            $where=array('order_id'=>$dat->order_id,'pr_id'=>$pro_id);
            $aabb=$this->model_select->select_where($where,'order_products');   

            if($aabb->num_rows()!=0){
                foreach($aabb->result() as $res){ $ii++; }
            }//-----forech end 
            $a[$month] = $ii;
        }

        $data['values']=$a;     
        $this->load->view('admin/reports',$data);
    }

$data传递视图页面如下:

$this->load->view('admin/reports',$data);
控制器加载该函数时提到

错误,该行如下:

$data['values']=$a; //error mention this line as below

错误:

Fatal error: Cannot use object of type CI_DB_mysql_result as array in controllers\admin.php on line 1286

我的代码意味着错误。请帮帮我。

1 个答案:

答案 0 :(得分:0)

现在我已经解决了,$data是由$data = $this->model_select->date_range($fdate,$fmonth,$tdate,$tmonth,$year);设置的CI_DB_mysql_result对象

为数据($data['values']=$a;)分配$ a时,会导致问题。

你可以尝试:

//unset the data var that contain the MySQL result
unset($data);
//setup the data vars to go to the view
$data=array('values'=>$a);

完整代码:

if($this->input->post('getit'))
{
    $pro_id=$this->input->post('product').'<br>';       
    $fdate= date('d', strtotime($this->input->post('from')));
    $fmonth = date('m', strtotime($this->input->post('from')));
    $tdate = date('d', strtotime($this->input->post('to')));
    $tmonth = date('m', strtotime($this->input->post('to')));

    $year= $this->input->post('year');

    $data = $this->model_select->date_range($fdate,$fmonth,$tdate,$tmonth,$year); 

    $ii=0;
    foreach ($data->result() as $dat){
        $dat->order_id;$month=date('m', strtotime($dat->order_date));

        $where=array('order_id'=>$dat->order_id,'pr_id'=>$pro_id);
        $aabb=$this->model_select->select_where($where,'order_products');   

        if($aabb->num_rows()!=0){
            foreach($aabb->result() as $res){ $ii++; }
        }//-----forech end 
        $a[$month] = $ii;
    }

    //unset the data var that contain the MySQL result
    unset($data);
    //setup the data vars to go to the view
    $data=array('values'=>$a);     
    $this->load->view('admin/reports',$data);
}