Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Is there anything else assigned to $data? –  sgt 2 hours ago
    
have you fetched the result set? used ->result()? –  Ghost 2 hours ago
    
Please provide your model function.. –  Eko Junaidi Salam 2 hours ago
    
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'); } –  Uraj 2 hours ago
    
you should edit your question, providing the model methods and the usage inside your controller –  Ghost 2 hours ago

1 Answer 1

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.