David David - 3 months ago 21
PHP Question

Call to a member function record_count() on null codeigniter

Trying to set up some pagination from a database that has a list of members in it. I had a query that returned the results but I could not incorporate it into the pagination example I found on the internet. Now i get a Call to a member function record_count() on null error when I try to load my memberlist. Below is the code.

Model

public function count_members()
{
return $this->db->count_all("Users");
}

public function getMembers($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get("users");

if ($query->num_rows() > 0){
foreach ($query->result() as $row) {
$data[] = $row;
}

return $data;
}
return false;
/*if ($id == null) {
$query = $this->db->get('users');
}else{
$query = $this->db->get_where('users', ['id' => $id]);
}
return $query->result(); */
}


View

<h1>Member Listing</h1>
<table border="1" align="center">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Status</th>
<th>Last Login</th>
<th>Delete</th>
<th>Write Up</th>
<thead>
<tbody>
<?php foreach ($members as $_key => $_value): ?>
<tr>
<td><?=$_value->first_name?></td>
<td><?=$_value->last_name?></td>
<td><?=$_value->email?></td>
<td><?=$_value->status?></td>
<td><?=$_value->last_login?></td>
<td><a href="<?=site_url("admin/delete_member/{$_value->id}");?>">[X]</a></td>
</tr>
<?php endforeach;?>
<?php echo $links; ?></p>
</tbody>
</table>
<a href="<?php echo base_url(); ?>admin/home">Back to Dashboard</a>


Controller

public function view_members()
{
$this->load->model('Users_model');
$config = array();
$config["base_url"] = base_url() . "admin/view_members";
$config["total_rows"] = $this->Users->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 3;

$this->pagination->initialize($config);

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Users->getMembers($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();

//$this->data['members'] = $this->Users_model->getMembers();
$this->load->view('admin/inc/admin_header');
$this->load->view('admin/view_members', $this->data);
$this->load->view('admin/inc/admin_footer');
}


Error Messages
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Admin::$users

Filename: controllers/Admin.php

Line Number: 217

Fatal error: Call to a member function count_members() on null in G:\xampp\htdocs\highball061516\application\controllers\Admin.php on line 217
A PHP Error was encountered

Severity: Error

Message: Call to a member function count_members() on null

Filename: controllers/Admin.php

Line Number: 217

Answer

In code igniter pagnation, $config["total_rows"] parameter requires total number of records exists in database. In your code, it was getting this count form a function that doesn't exists in model. It should get it from count_members instead. Here is the updated controller code:

public function view_members()
{
    $this->load->model('Users_model');
    $config = array();
    $config["base_url"] = base_url() . "admin/view_members";

    // Code modified here
    $config["total_rows"] = $this->Users_model->count_members();

    $config["per_page"] = 20;
    $config["uri_segment"] = 3;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["results"] = $this->Users->getMembers($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();

    //$this->data['members'] = $this->Users_model->getMembers();
    $this->load->view('admin/inc/admin_header');
    $this->load->view('admin/view_members', $this->data);
    $this->load->view('admin/inc/admin_footer');
}
Comments