Codeigniter分页在其他页面不起作用

时间:2016-06-19 21:03:00

标签: php codeigniter pagination

我在CI 3.0中启动了一个Web应用程序,一切顺利,我的分页工作正常,但有一个我无法弄清楚的问题......

我的模特

//Model
    function jumlah(){
        return $this->db->count_all('buku');
    }

    function fetch_buku($limit,$start){
      $this->db->limit($limit,$start);
      $query=$this->db->get('buku');

      if($query->num_rows()>0){
        foreach ($query->result() as $row) {
          $data[]=$row;
        }
        return $data;
      }
      return false;
    }

我的控制器

//Controller
    function index(){

        //load data
        $data['title']="Data Buku";

        $config['base_url']=site_url('anggota/index/');
        $total_row=$this->m_buku->jumlah();
        $config['total_rows']=$total_row;
        $config['per_page']=$this->limit;
        $config['use_page_numbers']=TRUE;
        $config['uri_segment']=3;
        $this->pagination->initialize($config);

        $page=($this->uri->segment(3)) ? $this->uri->segment(3):0;;

        $data['buku']=$this->m_buku->fetch_buku($config['per_page'],$page);
        $data['pagination']=$this->pagination->create_links();
            $data['message']='';
            $this->template->display('buku/index',$data);
    }

我的观点

//View


    <?php echo $message;?>
        <Table class="table table-striped">
    <thead>
        <tr>
            <td>No.</td>
            <td>Image</td>
            <td>Kode Buku</td>
            <td>Judul</td>
            <td>Pengarang</td>
            <td>Klasifikasi</td>
            <td colspan="2"></td>
        </tr>
    </thead>
    <?php $no=0; foreach($buku as $row ): $no++;?>
    <tr>
        <td><?php echo $no;?></td>
        <td><img src="<?php echo base_url('assets/img/'.$row->image);?>" height="100px" width="100px"></td>
        <td><?php echo $row->kode_buku;?></td>
        <td><?php echo $row->judul;?></td>
        <td><?php echo $row->pengarang;?></td>
        <td><?php echo $row->klasifikasi;?></td>
        <td><a href="<?php echo site_url('buku/edit/'.$row->kode_buku);?>"><i class="glyphicon glyphicon-edit"></i></a></td>
        <td><a href="#" class="hapus" kode="<?php echo $row->kode_buku;?>"><i class="glyphicon glyphicon-trash"></i></a></td>
    </tr>
    <?php endforeach;?>
</Table>

        <?php echo $pagination;?>

我的问题是当我加载其他页面时,数据无法在该页面显示。但数据可以显示在第一页。

2 个答案:

答案 0 :(得分:0)

这样做

在控制器中

$total_row = $this->m_buku->jumlah();

$config['base_url'] = base_url() . 'anggota/index/'; # make sure site is load without index.php
$config['total_rows'] = $total_row;
$config['per_page'] = '12'; # provide some number here
$config['uri_segment'] = 2; # this should be 2. If index.php is there then 3

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

$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

$data['links'] = $this->pagination->create_links();

在视图中

echo $links
  
    
      

确保网站加载时没有index.php

    
  

答案 1 :(得分:0)

如果将$config['use_page_numbers']设置为TRUE,则将其转换为偏移量。尝试将值更改为FALSE。假设您的$config['per_page']为10,而不是在您的网址中显示类似内容:anggota/index/anggota/index/10anggota/index/20。如果设置为TRUE,则您的网址可能会显示为anggota/index/1anggota/index/2anggota/index/3等。