Page pagination

Ditanyakan pada tanggal 17 November 2019 oleh Asep Hermawan

Dear,

Tabel data user sudah muncul. akan tetapi navigasi link tidak sesuai. misal untuk page 2 akan tetapi sewaktu di klik munculnya page 3

ada kesalahan di bagian mana yaaa,

terimaksih. /asep

Comment ..

Bisa share kode controller nya mas?

Dikomentari pada tanggal 19 November 2019 oleh Ahmad Oriza Sahputra
Comment ..

Terlampir File . controller, view dan MY_model (paginate)

File User.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {

public function __construct()
{
    parent::__construct();
    //Do your magic here
}

public function index($page =   null)
{
    $data['title']      =   'Admin : Pengguna';
    $data['content']    =   $this->user->paginate($page)->get();
  //$data['content']    =   $this->user->get();

    $data['total_rows'] =   $this->user->count();
    $data['pagination'] =   $this->user->makePagination(
        base_url('user'), 3, $data['total_rows']
    );
    $data['page']       =   'page/user/index';

    $this->view($data);
}

}

/ End of file User.php /

?>


File page/user/index.php

<main role="main" class="container">
    <?php $this->load->view('layouts/_alert')?>
    <div class="row">
        <div class="col-md-10 mx-auto">
            <div class="card mb-3">
                <div class="card-header">
                    <span>Pengguna</span>
                    <a href=<?= base_url('user/create') ?> class="btn btn-sm btn-secondary">Tambah</a>

                    <div class="float-right">
                        <form action="">
                            <div class="input-group">
                                <input type="text" name="keyword" class="form-control form-control-sm text-center" placeholder="Cari">
                                <div class="input-group-append">
                                    <button class="btn btn-info btn-sm" type="submit">
                                        <i class="fas fa-search"></i>
                                    </button>
                                    <a href="#" class="btn btn-info btn-sm">
                                        <i class="fas fa-eraser"></i>
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">Pengguna</th>
                                <th scope="col">E-Mail</th>
                                <th scope="col">Role</th>
                                <th scope="col">Status</th>
                                <th scope="col"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $no=0; foreach ($content as $row) :  $no++ ?>
                            <tr>
                                <td><?php echo $no ?></td>
                                <td>
                                    <p>
                                        <img src="https://placehold.co/70x70" alt="">
                                        Admin
                                    </p>
                                </td>
                                <td><?php  echo $row->email ?></td>
                                <td><?php echo $row->role ?></td>
                                <td>Aktif</td>
                                <td>
                                    <a href="#">
                                        <button class="btn btn-sm">
                                            <i class="fas fa-edit text-info"></i>
                                        </button>
                                    </a>
                                    <button class="btn btn-sm" type="submit">
                                        <i class="fas fa-trash text-danger"></i>
                                    </button>
                                </td>
                            </tr>
                            <?php endforeach ?>
                        </tbody>
                    </table>

                    <nav aria-label="Page navigation example">
                        <?= $pagination ?>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</main>

File MY_Model.php ( pada folder core)

public function paginate($page)
{
    $this->db->limit(
        $this->perPage,
        $this->calculateRealOffset($page)
    );
    //$this->db->get($this->table,2,$this->uri->segment(3));
    return $this;
}

public function calculateRealOffset($page)
{
    if($page == null){
        $offset = 0;
    } else {
        $offset = ($page*$this->perPage) - $this->perPage ;
    }
    return $offset;
}

public function makePagination($baseUrl, $uriSegment, $totalRows = null)
{
    $this->load->library('pagination');

    $config = [
        'base_url'      => $baseUrl,
        'uri_segment'   =>  $uriSegment,
        'per_page'      =>  $this->perPage,
        'total_rows'    =>  $totalRows,
        'use_page_number'   =>  true,

        'full_tag_open'     => '<ul class="pagination">',
        'full_tag_close'    => '</ul>',
        'attributes'        =>  ['class' => 'page-link'],

        'first_link'        =>  false,
        'last_link'         =>  false,
        'first_tag_open'    =>  '<li class ="page-item">',
        'first_tag_close'   =>  '</li>',

        'prev_link'         =>  '&laquo',            
        'prev_tag_open'     =>  '<li class="page-item">',
        'prev_tag_close'    =>  '</li>',

        'next_link'         =>  '&raquo',
        'next_tag_open'     =>  '<li class="page-item">',
        'next_tag_close'    =>  '</li>',

        'last_tag_open'    =>  '<li class="page-item">',
        'last_tag_close'   => '</li>',

        'cur_tag_open'      =>  '<li class="page-item active"><a href="#" class="page-link">',
        'cur_tag_close'     =>  '<span class="sr-only">(current)</span></a></li>',

        'num_tag_open'      =>  '<li class="page-item">',
        'num_tag_close'     =>  '</li>',
    ];

    $this->pagination->initialize($config);        
    return $this->pagination->create_links();
}
Dikomentari pada tanggal 19 November 2019 oleh Asep Hermawan
Comment ..

Maaf Mas :

untuk coding data pagination sudah di rubah-rubah :

" $data['pagination'] = $this->user->makePagination( base_url('user'), 2, $data['total_rows'] ); "

hasilnya tetap sama, Jadi pada navigasi halaman 1,2,3 >

untuk halaman 2 offsetnya 3, untuk hal 3 offsetnya 6 dst..

Minta tolong di pandu pak.. termaksih

Dikomentari pada tanggal 19 November 2019 oleh Asep Hermawan
Comment ..

Pada Bagian router.php juga sdh di update menjadi " $route['category/(:num)'] = 'category/index/$1'; $route['product/(:num)'] = 'product/index/$1'; $route['user/(:num)'] = 'user/index/$1'; " rgds, /asep

Dikomentari pada tanggal 19 November 2019 oleh Asep Hermawan

1 JAWABAN / 5 KOMENTAR

Generic placeholder image
Ahmad Oriza Sahputra menjawab pada tanggal 4 December 2019

Hai,

Dari kodenya sepertinya sudah terlihat sesuai dengan tutorial. Coba langkah-langkah berikut :

Pertama

Ganti bagian parameter segment pada kode. Biasanya page ngaco karena segment yang menjadi patokan offset salah.

Kedua

Sepertinya config use page numbers tidak terbaca, di mas kodenya salah harusnya user_page_numbers pakai s.

public function makePagination($baseUrl, $uriSegment, $totalRows = null)
{
    $this->load->library('pagination');

    $config = [
        'base_url'      => $baseUrl,
        'uri_segment'   =>  $uriSegment,
        'per_page'      =>  $this->perPage,
        'total_rows'    =>  $totalRows,
        'use_page_number'   =>  true,  // Bagian ini cek, harusnya use_page_numbers
 ...
Comment ..

noted , solved

terimaksih

Dikomentari pada tanggal 5 December 2019 oleh Asep Hermawan