PHP code example of kodhe / pagination

1. Go to this page and download the library: Download kodhe/pagination library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kodhe / pagination example snippets




declare(strict_types=1);

Pagination;

$config = [
    'base_url'   => 'http://localhost/artikel/index/',
    'total_rows' => 200,          // total seluruh baris data
    'per_page'   => 10,           // baris per halaman
    'num_links'  => 2,            // jumlah link tetangga di kiri/kanan halaman aktif
    'uri_segment'=> 3,            // posisi segmen URI yang memuat nomor halaman
];

$pagination = new Pagination($config);

echo $pagination->create_links();
// <ul class="pagination">... <li><a href=".../index/2">2</a></li> ...</ul>

$pagination = new Pagination();
$pagination->initialize([
    'base_url'   => site_url('artikel/index/'),
    'total_rows' => $this->artikel_model->count_all(),
    'per_page'   => 10,
    'cur_page'   => (int) $this->uri->segment(3), // opsional; default dibaca dari uri_segment
]);

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

$pagination = new Pagination([
    'base_url'             => 'http://localhost/artikel',
    'total_rows'           => 200,
    'per_page'             => 10,
    'page_query_string'    => true,
    'query_string_segment' => 'page',   // default 'per_page', ubah ke 'page'
    'use_page_numbers'     => true,
]);
echo $pagination->create_links();       // http://localhost/artikel?page=2

$pagination = new Pagination();
$pagination->renderer_name = 'bootstrap';  // atau 'tailwind', default 'default'
$pagination->initialize($config);
echo $pagination->create_links();

$pagination->initialize([
    'base_url'        => 'http://localhost/produk/index/',
    'total_rows'      => 500,
    'per_page'        => 25,
    'first_link'      => '&laquo; Awal',
    'last_link'       => 'Akhir &raquo;',
    'next_link'       => 'Berikutnya',
    'prev_link'       => 'Sebelumnya',
    'full_tag_open'   => '<nav class="mx-auto">',
    'full_tag_close'  => '</nav>',
    'cur_tag_open'    => '<span class="badge bg-primary">',
    'cur_tag_close'   => '</span>',
    'display_pages'   => true,
    'anchor_class'    => 'link-underline',
]);

echo render_pagination(site_url('artikel'), 200, 10, 'page');

src/
├── Pagination.php              # Kelas utama (API kompatibel CI3)
├── Contracts/                  # RendererInterface, UrlBuilderInterface
├── Renderers/                  # DefaultRenderer, BootstrapRenderer, TailwindRenderer
├── Url/                        # SegmentUrlBuilder, QueryStringUrlBuilder
├── Factory/RendererFactory.php
├── Support/                    # PaginationBuilder, AttributeHelper, LinkCache
├── ValueObjects/               # LinkData, PaginationConfig
├── language/pagination_lang.php
└── helpers.php                 # render_pagination() — helper Bootstrap 5 cepat