nushrat
nushrat

Reputation: 87

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

Upvotes: 1

Views: 194

Answers (4)

safin chacko
safin chacko

Reputation: 1390

Use

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

Upvotes: 0

Vinie
Vinie

Reputation: 2993

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]);
    }
}

Upvotes: 2

user5907059
user5907059

Reputation:

use

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

Upvotes: 1

paspo
paspo

Reputation: 1

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!

Upvotes: 0

Related Questions