Codeigniter 3 中的分页

Pagination in Codeigniter 3

我正在尝试为查询创建分页。我不明白为什么分页不起作用,它显示分页 links,现在我有 15 条记录,我想以 5 条为一组显示,但在页面 # 1 我看到了 15 条记录,当我点击第二页时 link 我有一个错误提示该页面不存在。

这是我的模型"searchCases":

//Function to get the active cases  
function getCases(){
    $query = $this->db->
        select('*')->
        from('customer_complaint')->
        where('active', 1)->
        get();
    return $query->result();

}

//获取打开案例数的函数

function countCases(){
    $query = $this->db->
        select('*')->
        from('customer_complaint')->
        where('active', 1)->
        get();
    return $query->num_rows();
}

// 函数检索 table 参数 $limit 和 $start 的所有记录列表 // 确定 return 的记录数以及从什么记录开始。

function fetchCases($limit, $start){
    $this->db->limit($limit, $start);
    $query = $this->db->
        select('*')->
        from('customer_complaint')->
        where('active', 1)->
        get();
        if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;

} }

现在这是我的控制器 "opened_cases":

public function index(){
    $this->load->view('headers');
    $this->load->view('navbar');        
    $data['query'] =  $this->searchCases->getCases();       
    $config['base_url'] = base_url().'index.php/opened_cases/';
    $config['total_rows'] = $this->searchCases->countCases();
    $config['per_page'] = 5;
    $config['uri_segment'] = 2;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] ="</ul>";
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['next_tag_open'] = "<li>";
    $config['next_tagl_close'] = "</li>";
    $config['prev_tag_open'] = "<li>";
    $config['prev_tagl_close'] = "</li>";
    $config['first_tag_open'] = "<li>";
    $config['first_tagl_close'] = "</li>";
    $config['last_tag_open'] = "<li>";
    $config['last_tagl_close'] = "</li>";
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $data["results"] = $this->searchCases->
        fetchCases($config["per_page"], $page); 
    $this->load->view('opened_cases', $data);
    $this->load->view('footer');
}

和我的观点 "opened_cases":

<div class="col-sm-12">
    <div class="panel panel-default">
    <div class="panel-heading">Search Results:
    </div><!--End of Panel Heading-->
    <div class="panel-body">
    <table class="table" style="text-align: center">
        <tr>
            <td>Case ID</td>
            <td>Received Date</td>
            <td>Folio</td>
            <td>Transactions Date</td>
            <td>Reason of Complaint</td>
            <td>Source of Complaint</td>
            <td>Files</td>          
        </tr>
        <?php
            foreach ($query as $row) {?>
            <tr>
                <td><a href="<?= base_url('/index.php/ticketView?id='.$row->complaint_id) ?>"><?= $row->complaint_id?></a></td>
                <td><?= date('m/d/Y', strtotime($row->received_date))?></td>
                <td><?= $row->folio?></td>
                <td><?= date('m/d/Y', strtotime($row->tx_date))?></td>
                <?php
                $this->load->model('searchCases');
                $id=$row->complaint_id;
                $reason_id=$this->searchCases->reason($id);
                $source_id=$this->searchCases->source($id);
                foreach ($reason_id as $rsn) {
                    if($rsn->reason == 'Others'){
                        echo "<td class='text-left'>".$rsn->reason.": ".$row->other_reason."</td>";
                    }else{
                    echo "<td class='text-left'>".$rsn->reason."</td>";
                }
                }
                foreach ($source_id as $src) {
                    echo "<td>".$src->source."</td>";
                }
                ?>
                <td><?php
                    $folder = $row->complaint_id;
                    $path_root = "upload/".$folder."/";
                    $dir = scandir($path_root, 1);
                    $array_lenght = count($dir)-2;
                    if($array_lenght>=1){?>
                        <span class="glyphicon glyphicon-paperclip"></span>
                    <?php } ?></td>
            </tr>
        <?php }?>
    </table>
    </div><!--End of body panel-->
    <div class="panel-footer text-center">
    </div><!--End of Panel Footer-->
    </div><!--End of Panel-->
    <div class="col-sm-12 text-center">
        <?= $this->pagination->create_links() ?>
    </div>  <!-- End of pagination -->
</div>

这是我的视图图像:

15 records instead of 5

更新:

我意识到我的错误:

$data['query'] =  $this->searchCases->getCases(); //I deleted this line, because i was getting all the results of the query.

现在在视图中,我在 foreach 循环中替换了 $results 而不是 $query,现在我可以看到我要求的每页 5 个结果。

foreach ($query as $row) {?>
            <tr>...

更改为:

foreach ($results as $row) {?>
            <tr>...

但是现在,当我点击第二页的 link 时,出现了与开始时相同的错误 404。

试试这个 $config['base_url'] = base_url().'index.php/opened_cases/index'; 并更改此 $config['uri_segment'] = 3;