Reputation: 1
I am building a polling web site with Codeigniter PHP frame work. But I am currently stuck as I can't really figure out where the bug is found in the script.
The website has an admin end where I need to tabulate and paginate the records I'll be fetching from the database tables, hence I created a Model method to pull records and paginate them. But it seems to have issues as certain records are not displayed at all even though my pagination links are displayed.
Here is my code:
## this is a general method for ALL data selection operations and pagination ##
public function select_db_data_paginate($table_name, $array_attributes){
//pagination configuration
//configuration details for pagination
$config['base_url'] = base_url().$array_attributes['base_url'];
$config['total_rows'] = $this->db->count_all('naphs_roles');
$config['per_page'] = $array_attributes['per_page'];
$config['uri_segment'] = 3;
$config['display_pages'] = TRUE;
//PAGINATION STYLING
$config['prev_link'] = '❮ Prev';
$config['next_link'] = 'Next ❯';
$config['attributes'] = array('class' => 'w3-button');
//initialize pagination
$this->pagination->initialize($config);
//where to start fetching DB records
$start_from = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//fetch the records
//$query = $this->db->get($table_name, $config['per_page'], $start_from);
$query = $this->db->get($table_name,$config['per_page'], $start_from);
$result = $query->result_array();
return $result;
}
And here is how I used the above method on one of my controllers
//fetch results based on the limit and start point. this data will be used to draw a table
$data['naphs_members'] = $this->Naphs_Model->select_db_data_paginate('naphs_members',array('base_url' => 'index.php/members/manage', 'per_page' => 2));
//create the pagination links
$data['pagination_links'] = $this->pagination->create_links();
//This data will be used to draw a table
$naphs_members = $this->Naphs_Model->select_db_data_paginate('naphs_members', array('base_url' => 'index.php/members/manage', 'per_page' => 2));
And here is another controller method which uses the pagination method created in the model above:
//fetch results based on the limit and start point. this data will be used to draw a table
$data['naphs_roles'] = $this->Naphs_Model->select_db_data_paginate('naphs_roles',array('base_url' => 'index.php/roles/manage', 'per_page' => 2));
//create the pagination links
$data['pagination_links'] = $this->pagination->create_links();
//This data will be used to draw a table
$naphs_roles = $this->Naphs_Model->select_db_data_paginate('naphs_roles',array('base_url' => 'index.php/roles/manage', 'per_page' => 2));
Finally, these are my routes for the pages (methods) :
$route['members/manage/(:any)'] = 'admin/Naphs_Members/manage/$1';
$route['roles/manage/(:any)'] = 'admin/Naphs_Roles/manage/$1';
The views are both working perfectly and the pagination links are displaying well. However, when the first link (page 1) of each pagination is clicked, a 404 page is shown.
Also, a table of 6 records only shows 4 and a table of 4 records, shows only 2... and so on.
If needed, my screenshots will be posted here. Thank you!
Upvotes: 0
Views: 131
Reputation: 1
Resolved! I saw the issue and fixed it. In the Model method, I was mistakenly fetching the total table rows from a "fixed" table instead of using $table_name parameter in the method. So the option:
$config['total_rows'] = $this->db->count_all('naphs_roles');
Was changed to :
$config['total_rows'] = $this->db->count_all($table_name);
Where $table_name is the first parameter in the Model method. Thank you all!
Upvotes: 0