簡體   English   中英

如何使用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(); 
    // ...
}

有關更多信息,請參見加載模型

嘗試這樣。您將返回count的整數值,因此無需使用控制器中的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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM