[英]How Select and view count rows from database with Codeigniter
This is my controller : Admin 这是我的控制器:管理员
class Admin extends CI_Controller {
public function index()
{
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
$this->load->view('Admin_view',$data);
}
}
This my model : Dash_model 这是我的模型:Dash_model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
This my view : Admin_view 我的观点:Admin_view
<?php echo $jumlah_instansi; ?>
Please help me, sorry newbie..Thank you.. 请帮助我,对不起新手..谢谢
Thats show error Message: Undefined property: Admin::$Dash_model Filename: controllers/Admin.php and Message: Call to a member function jml_instansi() on a non-object 多数民众赞成显示错误消息:未定义的属性:Admin :: $ Dash_model文件名:controllers / Admin.php和消息:调用非对象上的成员函数jml_instansi()
You must first load a model before accessing it, eg 您必须在访问模型之前首先加载模型,例如
public function index()
{
$this->load->model('Dash_model');
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
// ...
}
See Loading a Model for more. 有关更多信息,请参见加载模型 。
Try like this.You are returning integer value of count so no need to use result()
result set in controller. 尝试这样。您将返回count的整数值,因此无需使用控制器中的
result()
结果集。
Controller: 控制器:
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);
}
}
Model 模型
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
View: 视图:
<?php echo $jumlah_instansi; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.