简体   繁体   中英

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

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

This my 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

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.

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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM