Select 和视图如何使用 Codeigniter 从数据库中计算行数

How Select and view count rows from database with Codeigniter

这是我的控制器:管理员

class Admin extends CI_Controller {

public function index()
{
    $data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result(); 
    $this->load->view('Admin_view',$data);
}
}

这是我的模型:Dash_model

public function jml_instansi()
{       
    $query = $this->db->query("SELECT * FROM instansi");
    return $query->num_rows();
}}

这是我的观点:Admin_view

<?php echo $jumlah_instansi; ?>

请帮助我,对不起新手..谢谢..

那是显示错误 消息:未定义 属性:Admin::$Dash_model 文件名:controllers/Admin.php 和 消息:在非对象

上调用成员函数 jml_instansi()

您必须先加载模型才能访问它,例如

public function index()
{
    $this->load->model('Dash_model');
    $data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result(); 
    // ...
}

有关更多信息,请参阅 Loading a Model

像 this.You 那样尝试返回计数的整数值,因此无需在控制器中使用 result() 结果集。

控制器:

class Admin extends CI_Controller {

public function index()
{
    $this->load->model('Dash_model');//load model
    $data['jumlah_instansi'] = $this->Dash_model->jml_instansi();
    $this->load->view('Admin_view',$data);
}
}

型号

public function jml_instansi()
{       
    $query = $this->db->query("SELECT * FROM instansi");
    return $query->num_rows();
}}

查看:

<?php echo $jumlah_instansi; ?>