user3653474
user3653474

Reputation: 3854

Codeigniter Pagination does not work when clicked on further page link

I am working on Codeigniter Pagination and following this link :

http://www.technicalkeeda.com/codeigniter-tutorials/pagination-using-php-codeigniter

but when i click on further links i.e. 1,2 and so on then the next sequence of records does not loads up only the initial (from 1 to 5 shows up) i have seen other links on stackoverflow : CodeIgniter Pagination Page Links Not Working but it did not work. My code is as follows, please help to solve my problem:

Controller: home.php

<?php
class Home extends CI_Controller {


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


 public function index($offset=0){

  $config['total_rows'] = $this->MovieModel->totalMovies();

  $config['base_url'] = base_url().'/home/index';
  $config['per_page'] = 5;
  $config['uri_segment'] = '2';

  $config['full_tag_open'] = '<div class="pagination"><ul>';
  $config['full_tag_close'] = '</ul></div>';

  $config['first_link'] = '« First';
  $config['first_tag_open'] = '<li class="prev page">';
  $config['first_tag_close'] = '</li>';

  $config['last_link'] = 'Last »';
  $config['last_tag_open'] = '<li class="next page">';
  $config['last_tag_close'] = '</li>';

  $config['next_link'] = 'Next →';
  $config['next_tag_open'] = '<li class="next page">';
  $config['next_tag_close'] = '</li>';

  $config['prev_link'] = '← Previous';
  $config['prev_tag_open'] = '<li class="prev page">';
  $config['prev_tag_close'] = '</li>';

  $config['cur_tag_open'] = '<li class="active"><a href="">';
  $config['cur_tag_close'] = '</a></li>';

  $config['num_tag_open'] = '<li class="page">';
  $config['num_tag_close'] = '</li>';


  $this->pagination->initialize($config);


  $query = $this->MovieModel->getMovies(5,$this->uri->segment(2));

  $data['MOVIES'] = null;

  if($query){
   $data['MOVIES'] =  $query;
  }

  $this->load->view('index1.php', $data);
 }
}
?>

MovieModel.php

<?php
class MovieModel extends CI_Model {


 function getMovies($limit=null,$offset=NULL){
  $this->db->select("MOVIE_ID,FILM_NAME,DIRECTOR,RELEASE_YEAR");
  $this->db->from('trn_movies');
  $this->db->limit($limit, $offset);
  $query = $this->db->get();
  return $query->result();
 }

 function totalMovies(){
  return $this->db->count_all_results('trn_movies');
 }
}
?>

View: index1.php

<!DOCTYPE html>
 <html lang="en">
  <head>
   <title>Codeigniter Pagination Example</title>
   <link href="<?= base_url();?>css/bootstrap.css" rel="stylesheet">
  </head>
  <body>
   <div class="container">
    <div class="row">
     <div class="col-md-12">
      <div class="row">

       <h4>Movies List</h4>
       <table class="table table-striped table-bordered table-condensed">
        <tr><td><strong>Movie Id</strong></td><td><strong>Film Name</strong></td><td><strong>Director</strong></td><td><strong>Release Year</strong></td></tr> 
        <?php 
        if(is_array($MOVIES) && count($MOVIES) ) {
         foreach($MOVIES as $movie){     
        ?>
        <tr><td><?=$movie->MOVIE_ID;?></td><td><?=$movie->FILM_NAME;?></td><td><?=$movie->DIRECTOR;?></td><td><?=$movie->RELEASE_YEAR;?></td></tr>     
           <?php 
         }        
        }?>  
       </table>            
      </div>
     </div>
    </div>
    <div class="row">
              <div class="col-md-12">
      <div class="row"><?php echo $this->pagination->create_links(); ?></div> 
     </div>
    </div>
   </div>    
  </body>
 </html> 

Upvotes: 0

Views: 1312

Answers (1)

msvairam
msvairam

Reputation: 872

Use the below coding

if($this->uri->segment(3))

$offset = $this->uri->segment(3);

else 

$offset = 0;

 $query = $this->MovieModel->getMovies(5,$offset);

Upvotes: 1

Related Questions