mercredi 22 juin 2016

Pagination in Codeigniter 3

Im trying to make a pagination for a query. I cant figure why is the reason that the pagination doesn't work, it shows the pagination links, right now i have 15 records and i want to show that in batches of 5, but in the page # 1 i see the 15 records and when i click in the second page link i have an error that says that the page doesn't exist.

This is my model "searchCases":

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

}

//Function to get the number of opened cases

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

// Function to retrieve a list of all the records of the table an the params $limit and $start to // determine the number of records to return and what record to start from.

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;

} }

Now this is my Controller "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');
}

and my View "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>

Here is an image of my view

15 records instead of 5

UPDATE:

i realize my mistake:

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

and now in the view i replace $results instead $query in the foreach cycle and now i can see the 5 results per page that im asking for.

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

Changed to:

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

But now when i click on the link for the second page i have the same error 404 that i had from the start.

Aucun commentaire:

Enregistrer un commentaire