Uraj
Uraj

Reputation: 47

Codeigniter : Fatal error: Cannot use object of type CI_DB_mysql_result as array

i got an error when array values pass to view page from controller.

my code as below :

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

My Controller function:

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 passing view page as follows:

$this->load->view('admin/reports',$data);

error mentioned when controller load that function, that line as below:

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

error as :

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

i make mistake in my code means. please help me.

Upvotes: 0

Views: 2666

Answers (1)

Dale Hurley
Dale Hurley

Reputation: 151

Now I have worked it out, $data is an CI_DB_mysql_result object as set by $data = $this->model_select->date_range($fdate,$fmonth,$tdate,$tmonth,$year);

When you assign $a to data ($data['values']=$a;) you are causing the issue.

You could try:

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

Complete Code:

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

Upvotes: 0

Related Questions