0

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.

8
  • Is there anything else assigned to $data? Commented Apr 13, 2015 at 4:50
  • have you fetched the result set? used ->result()? Commented Apr 13, 2015 at 4:50
  • Please provide your model function.. Commented Apr 13, 2015 at 4:50
  • model function function date_range($fdate,$fmonth,$tdate,$tmonth,$year) { $this->db->select('*'); $this->db->where('order_date >=',date('Y-m-d',mktime(0,0,0,$fmonth,$fdate,$year))); $this->db->where('order_date <=',date('Y-m-d',mktime(0,0,0,$tmonth,$tdate,$year))); return $this->db->get('orders'); } Commented Apr 13, 2015 at 4:52
  • you should edit your question, providing the model methods and the usage inside your controller Commented Apr 13, 2015 at 4:54

1 Answer 1

0

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.