PHP code example of rgehan / paginator

1. Go to this page and download the library: Download rgehan/paginator 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/ */

    

rgehan / paginator example snippets



define('ITEMS_PER_PAGE', 3);

// Gets the count of articles
$articlesCount = 10; // You might want to load that from the database

// Makes pagination
$paginator = new Paginator($articlesCount, self::ITEMS_PER_PAGE);
$paginator->setURLFormatString("http://localhost/articles?page=%d");

// Gets the page we want to access
if(isset($_GET['page']))
    $page = $paginator->getValidPage($_GET['page']); // Returns a page in the range of the existing pages
else
    $page = 0;

// Fetches the articles
$startIndex = $page * self::ITEMS_PER_PAGE;
$articles = do_sql_query("SELECT * FROM articles LIMIT " . $startIndex . ", " . ITEMS_PER_PAGE); // Pseudo-code for SQL query

// Creates the pagination
$pagination = $paginator->generateLinks($page);

/*
    Let's say: $page = 2

    Outputs:
    [
        [
            'url' => 'http://localhost/articles?page=0',
            'index' => 0,
            'current' => false
        ],[
            'url' => 'http://localhost/articles?page=1',
            'index' => 1,
            'current' => false
        ],[
            'url' => 'http://localhost/articles?page=2',
            'index' => 2,
            'current' => true
        ],[
            'url' => 'http://localhost/articles?page=3',
            'index' => 3,
            'current' => false
        ],
        ...
    ]
 */