2017 © Pedro Peláez
 

library paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

image

voodoophp/paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  • Wednesday, July 22, 2015
  • by Mardix
  • Repository
  • 4 Watchers
  • 38 Stars
  • 1,124 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 7 Forks
  • 3 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

Paginator 2.x.x

A paginator that makes it easy and simple


Name: Paginator, (*1)

License: MIT, (*2)

Author: Mardix, (*3)

Version : 2.x.x, (*4)

Requirements: PHP >= 5.4, (*5)


About Paginator

Paginator is a simple class that allows you to create pagination for your application. It doesn't require any database connection. It only requires the total of items found and from there it will create a pagination that can be export to HTML or Array. It is also compatible with Twitter's Bootstrap Framework., (*6)


Install Paginator

You can just download Paginator as is, or with Composer., (*7)

To install with composer, add the following in the require key in your composer.json file, (*8)

"voodoophp/paginator": "2.*"

composer.json, (*9)

{
    "name": "voodoophp/myapp",
    "description": "My awesome Voodoo App",
    "require": {
        "voodoophp/paginator": "2.*"
    }
}

Create a Pagination

Paginator makes it easy to create pagination. It only requires the URL that contains the page number pattern, the total items, and the total of items per page., (*10)

http://site.com/search?q=flower&page=15

or, (*11)

http://site.com/genres/pop/page/15

Paginator will automatically match the page number with the url above, and create pagination for the rest of the pages based on the total items and items per page provided., (*12)

Catching the page number

Paginator requires the keyword (:num) in the page number pattern to match the page number in the URL., (*13)

Page number pattern are set in the following format, (*14)

For friendly URL, it will capture http://site.com/page/25, (*15)

/page/(:num)

For normal URL, it will capture http://site.com/?page=25, (*16)

page=(:num)

Page number pattern is set in, (*17)

Voodoo\Paginator::setUrl($url, $pagePattern);

On both examples it will catch the page number [25] and create from there the start and the end of the pagination., (*18)

Any variation of the page number pattern is OK, as long as it includes (:num), (*19)

A Simple Example

<?php

include "src/Voodoo/Paginator.php";

// Pretending the the request url is: 
// http://mysite.com.com/hip-hop/page/5

// to catch the page number pattern in the request url
$pagePattern = "/page/(:num)"; 

// Some pre-calculated number
$totalItems = 150; 

// Total items to show per page
$totalPerPage = 10; 

$paginator = (new Voodoo\Paginator($pagePattern))
                ->setItems($totalItems, $totalPerPage);
?>

<html>
    <head>
        <link rel="stylesheet" href="./assets/paginator.css">
        <title>Paginator Example</title>
    </head>

    <body>
        <?= $paginator; ?>
    </body>
</html>

Will render something like this:, (*20)

[First] [<< Prev] [1] [2] [3] [4] [5] [6] [Next >>] [Last]

With links that look like:, (*21)

<a href='http://mysite.com/hip-hop/page/6'>6</a>

Methods


Voodoo\Paginator::__construct()

setUrl($url, $pagePattern);


##### Alternative with the URL being set automatically

Paginator will set the URL automatically based on the request URI if setUrl() is not called. All the paginations will be based on the current url. 

In the example below, the __construct() accepts the pagePattern and  create the pagination url from the getUri();

    setUrl($url, $pagePattern)
                    ->setItems($totalItems, $totalPerPage);
     
---

####Voodoo\Paginator::setPage()

By default, Paginator will catch the page number from the URL, but if you want to set the current page manually, setPage let you do so

    setUrl($url, $pagePattern)
                    ->setItems($totalItems, $totalPerPage)
                    ->setPage(4);
                    
--- 

####Voodoo\Paginator::setPrevNextTitle()

To set the Prev and Next title when the pagination is long.
By default it is set to **Prev** and **Next**

    setUrl($url, $pagePattern)
                    ->setItems($totalItems, $totalPerPage)
                    ->setPage(4)
                    ->setPrevNextTitle("Prev",  "Next");
     
---

####Voodoo\Paginator::setFirstLastTitle()

To set the First and Last title when the pagination is long.
By default it is set to **First** and **Last**

    setUrl($url, $pagePattern)
                    ->setItems($totalItems, $totalPerPage)
                    ->setPage(4)
                    ->setPrevNextTitle("Prev",  "Next")
                    ->setFirstLastTitle("First", "Last");
                    
---
                    
####Array Voodoo\Paginator::toArray()

Will return the pagination data as an array which be used to create the pagination.

    setUrl($url, $pagePattern)
                    ->setItems($totalItems, $totalPerPage)
                    ->setPage(4)
                    ->setPrevNextTitle("Prev",  "Next")
                    ->setFirstLastTitle("First", "Last");
                    
     $pagination = $paginator->toArray();


toArray() will return an array similar to this:


    [
        [
            "page_number" => 1,
            "label" => "First",
            "url" => "http://mysite.com/pop/page/1",
            "is_current" => false,
            "is_first" => true,                
        ],
        [
            "page_number" => 1,
            "label" => "Prev",
            "url" => "http://mysite.com/pop/page/1",
            "is_current" => false,
            "is_prev" => true,                   
        ],  
        [
            "page_number" => 1,
            "label" => 1,
            "url" => "http://mysite.com/pop/page/1",
            "is_current" => true                   
        ],
        [
            "page_number" => 2,
            "label" => 2,
            "url" => "http://mysite.com/pop/page/2",
            "is_current" => false                   
        ],
        ...
        [
            "page_number" => "10",
            "label" => "Next",
            "url" => "http://mysite.com/pop/page/10",
            "is_current" => false,
            "is_next" => true,                     
        ],  
        [
            "page_number" => 15,
            "label" => "Last",
            "url" => "http://mysite.com/pop/page/15",
            "is_current" => false,
            "is_last" => true,                  
        ]   
    ]
    
### Manually create the pagination with toArray()

    
    toArray() as $page) : ?> <li class='<?= ($page["is_current"]) ? "active" : ""; ?>'> <a href='<?= $page["url"]; ?>'><?= $page["label"]; ?></a> </li> <?php endforeach; ?> </ul>

    String Voodoo\Paginator::toHtml()

    Will return a formatted HTML, compatible with Bootstrap framework, (*22)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPage(4)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 $cssClassName = "pagination";

 echo $paginator->toHtml($cssClassName);

String Voodoo\Paginator::__toString()

Same as toHtml(), except it's when someone is echoing the paginator object, (*23)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPage(4)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator;

Getters

Voodoo\Paginator::getUri()

Return the URI of the request (the url in the address bar). It can be used to automatically set the url based on the request uri., (*24)

In the example below, the __construct() accepts the pagePattern and create the pagination url from the getUri();, (*25)

<?php

include "src/Voodoo/Paginator.php";

$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator);

$uri = $paginator->getUri();

Voodoo\Paginator::getPage()

Get the current page number, (*26)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getPage(); // -> 4

Voodoo\Paginator::count()

Get the total pages, (*27)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->count(); // 15

Voodoo\Paginator::getPerPage()

Get total items per page, (*28)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getPerPage(); // 10

Voodoo\Paginator::getStart()

Get the start of the count for the pagination set, (*29)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getStart(); // 30

Voodoo\Paginator::getEnd()

Get the end of the count for the pagination set, (*30)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getEnd(); // 39

Voodoo\Paginator::getPageUrl()

Return the page url based on the url provided, (*31)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getPageUrl(); // http://mysite.com/pop/page/4

Also it can be used to get the url of any other pages, (*32)

$page6 = $paginator->getPageUrl(6); // http://mysite.com/pop/page/6

Voodoo\Paginator::getNextPageUrl()

Return the next page url, (*33)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getNextPageUrl(); // http://mysite.com/pop/page/5

Voodoo\Paginator::getPrevPageUrl()

Return the previous page url, (*34)

<?php

include "src/Voodoo/Paginator.php";

$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$totalItems = 150;
$totalPerPage = 10;

$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage)
                ->setPrevNextTitle("Prev",  "Next")
                ->setFirstLastTitle("First", "Last");

 echo $paginator->getPrevPageUrl(); // http://mysite.com/pop/page/3

Using Paginator with SQL Query

<?php
/**
 * Using Paginator with SQL query
 * 
 * We will execute two queries, one to count all entries, 
 * the second one to get all the data with LIMIT and OFFSET
 */

include(dirname(__DIR__)."/src/Voodoo/Paginator.php");

$pdo = new PDO ("mysql:host=$hostname;dbname=$dbname", "$username", "$pw");
$tableName = "myTable";

/**
 * To count all the entries based on the criteria provided
 */
$sth = $pdo->query("SELECT COUNT(id) AS count FROM {$tableName}");
$countResult = $sth->fetch(PDO::FETCH_ASSOC);

$totalItems = $countResult["count"];
$totalPerPage = 10;

/**
 * Paginator will allow us to get the LIMIT and OFFSET for the query
 */
$url = "http://mysite.com/pop/page/4";
$pagePattern = "/page/(:num)";
$paginator = (new Voodoo\Paginator)
                ->setUrl($url, $pagePattern)
                ->setItems($totalItems, $totalPerPage);

$limit = $paginator->getPerPage();
$offset = $paginator->getStart();
$results = $pdo->query("SELECT * FROM {$tableName} LIMIT {$limit} OFFSET {$offset}");

?>

<html>
    <head>
        <link rel="stylesheet" href="./assets/paginator.css">
        <title>Paginator SQL Example</title>
    </head>

    <body>
        <?= $paginator; ?>
    </body>
</html>

Please refer to the example directory for examples, (*35)


(c) This Year Mardix :), (*36)

The Versions

22/07 2015

dev-master

9999999-dev https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

page pagination

22/07 2015

2.1.1

2.1.1.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

page pagination

22/07 2015

2.1.0

2.1.0.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

page pagination

23/10 2013

dev-feat-bootstrap3

dev-feat-bootstrap3 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

page pagination

25/06 2013

2.0.0

2.0.0.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

page pagination

08/06 2013

1.1.2

1.1.2.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

page pagination

06/05 2013

1.1.1

1.1.1.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

page pagination

05/05 2013

1.1

1.1.0.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

page pagination

11/01 2013

1.0.4

1.0.4.0 https://github.com/mardix/Paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

page pagination