Codeigniter:致命错误:不能将CI_DB_mysql_result类型的对象用作数组 [英] 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.
我的代码如下:
$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
传递视图页面,如下所示:
$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
错误为:
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.
推荐答案
现在我已经解决了, $ data
是由 $ data = $ this->设置的CI_DB_mysql_result对象; model_select-> date_range($ fdate,$ fmonth,$ tdate,$ tmonth,$ year);
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);
当您将$ a分配给数据( $ data ['values'] = $ a;
)时,就会引起问题.
When you assign $a to data ($data['values']=$a;
) you are causing the issue.
您可以尝试:
//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);
}
这篇关于Codeigniter:致命错误:不能将CI_DB_mysql_result类型的对象用作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!