我正在学习codeigniter框架。我设法使登录页面正常工作,现在我正尝试将数据从控制器传递到我的视图。我遇到了我一直在查看和查看的错误,并按照建议进行操作,仍然收到错误/
模型
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
我做错了什么?
答案 0 :(得分:1)
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;
}
答案 1 :(得分:0)
当您已经知道将获得一行结果时,可以使用row()获得单行而不是result()
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->row()->totaltasks;
}