点击进一步的页面链接时,Codeigniter分页不起作用

时间:2016-03-02 09:22:35

标签: php codeigniter pagination

我正致力于Codeigniter分页并点击此链接:

  

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

但是当我点击其他链接,即1,2等等时,下一个记录序列不会仅加载初始(从1到5显示)我在stackoverflow上看到了其他链接:{{3}但它没有用。我的代码如下,请帮助解决我的问题:

  

控制器: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');
 }
}
?>
  

查看: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> 

1 个答案:

答案 0 :(得分:1)

使用以下编码

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

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

else 

$offset = 0;

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