我在这里检查了几个关于 CodeIgniter 中的分页的线程,但我还没有找到解决我的问题的方法。
我有一个位于 controllers/admin/index.php 的控制器类,名为“Index”,其中包含以下代码:
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' );
}
}
现在,如果我转到 localhost/cream/public_html/admin 会显示正确数量的结果,并且会呈现指向下一页的链接,但是当我单击下一步或转到第 2 或第 3 页时,我会收到 404 错误。
现在我尝试将我的基本网址更改为:
$config['base_url'] = base_url() . 'admin/index';
和:
$config['base_url'] = base_url() . 'admin/index/';
和:
$config['base_url'] = base_url() . 'admin/';
同时也将我的 uri_segment 分别更改为 1、2 和 3,但我得到了相同的结果。
我什至尝试过使用:
$route['admin/(:num)'] = 'admin/$1';
和:
$route['admin/(:num)'] = 'admin';
不过,我得到了 404 错误。
这些是我的“Event_Model”类中的模型函数:
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;
}
我有点卡在这个问题上,希望能得到您的帮助。谢谢。
编辑:添加 routes.php
$route['default_controller'] = "index";
$route['404_override'] = '';
$route['admin/(:num)'] = 'admin/index/$1';