我正在学习代码点火器框架。我已经设法使登录页面工作,现在我正试图将数据从控制器传递到我的视图。我得到了错误,我在这里看了又看,按照建议做了,仍然得到了错误/
型号
class Dashboard_Model extends CI_Model{
function __construct(){
parent::__construct();
}
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result();
}
}
控制器
class Dashboard extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('admin/dashboard_model');
}
public function index(){
if(!$this->session->logged_in){
redirect('user_login');
}else{
$data['title'] = $this->session->fullname. " | Dashboard";
$this->load->view('admin/common/header',$data);
$data['total'] = $this->dashboard_model->getTasks();
$this->load->view('admin/dashboard',$data);
$this->load->view('admin/common/footer');
}
}
}
查看
<h2 class="text-white"><span data-plugin="counterup"><?php echo $total?></span> <small><i class="mdi mdi-arrow-up text-success"></i></small></h2>
错误:
严重性:注意
消息:数组到字符串转换
文件名:admin/dashboard.php
线路编号:40
我做错了什么?
result()
此方法以对象数组的形式返回查询结果,或在失败更新getTasks
时返回空数组,如下面的
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result()[0]->totaltasks;
}
当您已经知道将获得一行作为结果时,可以使用row((来获得单行,而不是result((
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->row()->totaltasks;
}