CodeIgniter Paging 404

落花浮王杯 提交于 2019-12-23 03:32:00

问题


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

回答1:


$route['admin/(:num)'] = 'admin/index/$1'; needs to reside in config/routes.php- you cannot add routes to controller methods.




回答2:


OK, I've figured out what is going on.

When accessing a paged result other than the first page I was at the URI 'localhost/cream/public_html/admin/$1', where $1 represents the page number.

In my 'Admin' controller code, under the index() function, the '$config['uri_segment']' property value needed to be set to 2. This alone wouldn't have fixed the issue but it was the correct URI segment.

Next, I needed to add the following 2 routes to 'routes.php':

$route['admin'] = 'admin/admin';
$route['admin/:num'] = 'admin/admin';

The first route was because I had now changed my controller class name to 'Admin' and its filename to 'admin.php', so I needed a route that would work when the user was at the following URI:

localhost/cream/public_html/admin/

The second route was to reroute the page number to the correct controller, which was now 'admin/admin'.

That was it. My pagination is now working.

All I need to do now is get it to change the '$limit' variable so that it can be dynamically changed via a drop down. I should be able to pass the value of the dropdown to $_POST and pull it in to my 'get_paged_events();' function in my model. I'll let you know how I get on.

Cheers for all the help folks.




回答3:


To avoid index from your paging just follow the below step

Step1: customize /config/routes.php

   $route['controller/(:num)'] = "controller/index/$1";
   It convert your url www.yourdomian.com/controller/(number) to www.yourdomain.com/controller/index/(number)

Step2: Customize your pagination code with $config['uri_segment'] = 2 like below

        $config['base_url'] = base_url().'controller/';
        $config['display_pages'] = TRUE;
        $config['first_link'] = 'First';
        $config['total_rows'] = (your total rows);
        $config['uri_segment'] = 2;
        $this->pagination->initialize($config);
        echo $this->pagination->create_links();

cheers..




回答4:


Do you have url rewriting on your web server? If don't then try localhost/path/to/codeigniter/index.php/admin. If you want to skip the index.php then read about mod_rewrite (if you are using apache) and check the codeigniter's user guide for site_url function that might be useful to generate in-site urls.



来源:https://stackoverflow.com/questions/15227764/codeigniter-paging-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!