简体   繁体   中英

pagination links not working codeigniter

I am using the pagination class in my codeigniter application. The pagination links show up fine, ie, I suppose the right number of links eg. 1 2 3 4 5 >, and the page loads up with the amount of entries I specified per page.

The error is when I click on one of the links to go to the next set of entries, it says "page cannot be found".

controller

class Testimonials extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('testimonial_model');
        $this->load->library('pagination');
    }

    public function index(){
        $config=[
            'base_url'          =>  base_url('lalcoresidency/testimonials'),
            'per_page'          =>  15,
            'total_rows'        =>  $this->testimonial_model->num_rows(),           
            'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'     =>  '<li>',
            'first_tag_close'    =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
        ];
        $this->pagination->initialize($config);       
        $result=$this->testimonial_model->get_testimonial($config['per_page'],$this->uri->segment(3));        
        $this->load->view('testimonials_view',['result'=>$result]);
    }
}

model

class Testimonial_model extends CI_Model{

    function get_testimonial($limit,$offset){
        $this->db->select('*');
        $this->db->from('testimonial');
        $this->db->order_by("r_id", "desc");
        $this->db->limit($limit,$offset);
        $query=$this->db->get();
        return $result=$query->result();
    }

    public function num_rows(){
        $this->db->select('*');
        $this->db->from('testimonial');
        $this->db->order_by("r_id", "desc");

        $query=$this->db->get();
        return $result=$query->num_rows();
    }
}

My website testimonial link is

http://localhost/lalcoresidency/testimonials

You missed uri_segment in $config and change base_url as below. Try this

    class Testimonials extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('testimonial_model');
        $this->load->library('pagination');
    }

    public function index(){
        $config=[
            'base_url'          =>  base_url('testimonials/index'),
            'uri_segment'       => 3,
            'per_page'          =>  15,
            'total_rows'        =>  $this->testimonial_model->num_rows(),           
            'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'     =>  '<li>',
            'first_tag_close'    =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
        ];
        $this->pagination->initialize($config);       
        $result=$this->testimonial_model->get_testimonial($config['per_page'],$this->uri->segment(3));        
        $this->load->view('testimonials_view',['result'=>$result]);
    }
}

采用

'base_url'=>  base_url('testimonials/index'),

Maybe this has something to do with URL rewriting.

first check if

http://localhost/lalcoresidency/index.php/testimonials

is working.

If so, take a look at config.php:

$config['index_page'] = 'index.php';

If you enabled mod_rewrite and you want cleaner URLs (like the one you provided) you have to set the above variable to '' and configure an .htaccess as follow:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Hope this helps!

采用

'base_url'=>  base_url() . 'testimonials/index/',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM