I've checked several threads on here regarding paging in CodeIgniter but I haven't found a solution to my problem.
I have a controller class located at controllers/admin/index.php called 'Index' that contains the following code:
class Index extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper("url");
$this->load->helper( 'events_helper' );
$this->load->helper( 'delegates_helper' );
$this->load->model( 'admin/event_model' );
$this->load->model( 'admin/delegate_model' );
$this->load->library( 'pagination' );
}
public function index() {
$config = array();
$config['base_url'] = base_url() . 'admin';
$config['total_rows'] = $this->event_model->record_count();
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize( $config );
$page = ( $this->uri->segment( 2 ) ) ? $this->uri->segment( 2 ) : 0;
$data['paged_events'] = $this->event_model->get_paged_events( $config['per_page'], $page );
$data['links'] = $this->pagination->create_links();
$data['events'] = $this->event_model->get_events();
$data['event_courses'] = $this->event_model->get_event_courses();
$data['event_suppliers'] = $this->event_model->get_event_suppliers();
$data['event_venues'] = $this->event_model->get_event_venues();
$data['event_locations'] = $this->event_model->get_event_locations();
$data['event_statuses'] = $this->event_model->get_event_statuses();
$data['event_prices'] = $this->event_model->get_event_prices();
$data['event_types'] = $this->event_model->get_event_types();
$data['event_tutors'] = $this->event_model->get_event_tutors();
$data['event_delegates'] = $this->event_model->get_event_delegates();
$data['event_delegate_statuses'] = $this->event_model->get_event_delegate_statuses();
$this->load->view( 'templates/admin/header', $data );
$this->load->view( 'admin/index', $data );
$this->load->view( 'templates/admin/footer' );
}
}
Now if I go to localhost/cream/public_html/admin the correct amount of results show and the links to the next pages render but when I click next or go to page 2 or 3, I get a 404 error.
Now I've tried changing my base url to:
$config['base_url'] = base_url() . 'admin/index';
and:
$config['base_url'] = base_url() . 'admin/index/';
and:
$config['base_url'] = base_url() . 'admin/';
While also changing my uri_segment to 1, 2 and 3 respectively but I get the same result.
I've even tried using:
$route['admin/(:num)'] = 'admin/$1';
and:
$route['admin/(:num)'] = 'admin';
Still, I get the 404 error.
These are my model functions in my 'Event_Model' class:
public function record_count() {
$this->db->select_sum( 'event_active' );
$this->db->from( 'events' );
$this->db->where( 'event_active', 1 );
$query = $this->db->get();
$total_active_events = $query->row()->event_active;
if( $total_active_events > 0 ) {
return $total_active_events;
}
return false;
}
public function get_paged_events( $limit, $start ) {
$this->db->limit( $limit, $start );
$this->db->where( 'events.event_active = 1' );
$this->db->order_by( 'events.event_start_date', 'asc' );
$query = $this->db->get( 'events' );
if( $query->num_rows() > 0 ) {
foreach ( $query->result() as $row ) {
$data[] = $row;
}
return $data;
}
return false;
}
I'm a bit stuck on this one and would appreciate your help. Thanks.
EDIT: Added routes.php
$route['default_controller'] = "index";
$route['404_override'] = '';
$route['admin/(:num)'] = 'admin/index/$1';