How to Codeigniter Pagination to update specific part of a page

时间:2016-03-02 10:52:47

标签: php jquery codeigniter pagination codeigniter-2

I have 3 sections in my view, there is one section of a review I want that part of a view to change when user selects another page link that must refresh only the paging div. My code for simple Pagination in Codeigniter is as follows and works perfectly.

enter image description here

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'] = '3';

  $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);


  if($this->uri->segment(3))
  {
    $offset = $this->uri->segment(3);
  }
  else 
  {
     $offset = 0;


  }

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

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

  $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 :(得分:0)

单靠PHP无法做到这一点。

您必须使用AJAX更新页面上的部分。

建议的方法是:

  1. 使用jQuery检测按钮点击。
  2. 向您的Codeigniter后端发送请求。
  3. jQuery检测成功,使用后端打印的数据更新前端的容器。
  4. 请参阅:http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php