【问题标题】:Codeigniter Pagination does not work when clicked on further page link单击更多页面链接时,Codeigniter 分页不起作用
【发布时间】:2016-03-02 09:22:34
【问题描述】:

我正在使用 Codeigniter 分页并点击此链接:

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

但是当我点击更多链接时,例如 1,2 等等,下一个记录序列不会只加载初始(从 1 到 5 显示)我在 stackoverflow 上看到了其他链接:CodeIgniter Pagination Page Links Not Working 但是它不起作用。我的代码如下,请帮忙解决我的问题:

控制器: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> 

【问题讨论】:

  • 你能给我们举一个create_links();生成的链接的例子吗?
  • echo $this->db->last_query();在 getMovies() 模型中并检查偏移量和限制,同时单击第二个分页链接
  • @AdrienXL : 这是生成的链接 localhost/Myproject/home/index/5">2</a>
  • $query = $this->MovieModel->getMovies(5,$this->uri->segment(3));第三段是 5
  • 不是缺少http:// 部分吗?您还完成了从您的网址中删除 index.php 的必要措施吗?应该是http://localhost/Myproject/index.php/home/index/5。我猜

标签: php codeigniter pagination


【解决方案1】:

使用下面的编码

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

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

else 

$offset = 0;

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

【讨论】:

  • 谢谢,这是有效的,但第一个链接上没有链接,第二件事是当前链接没有突出显示。
  • 当前激活的链接添加了活动类。您将为活动课程应用样式
  • 但是如何处理带有空白 href=""的第一个链接
  • 很高兴为我提供帮助,您能否为我的答案投票,然后继续前进
  • 1 并点击不显示 1 到 5 记录如何解决此问题
猜你喜欢
  • 2014-11-20
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2016-11-29
相关资源
最近更新 更多