2017 © Pedro Peláez
 

library paginator

A generic PHP class to split large data into smaller chunks for use in web apps

image

kosinix/paginator

A generic PHP class to split large data into smaller chunks for use in web apps

  • Friday, November 4, 2016
  • by kosinix
  • Repository
  • 1 Watchers
  • 1 Stars
  • 511 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 80 % Grown

The README.md

Paginator

A generic PHP class to split large data into smaller chunks for use in web apps., (*1)

Requirements

  • PHP >= 5.3.3

Installation

Manual

  • Download the zip file from the Github repository.
  • Unpack the zip file and include the files in your project.
  • Include the class in /src/:
require_once '/path/to/src/Kosinix/Paginator.php'; // Change this to the correct path

Composer

Inside your project directory, open the command line and type:, (*2)

composer require kosinix/paginator:dev-master --prefer-dist

Include the autoload.php found in vendor/:, (*3)

require_once '/path/to/vendor/autoload.php'; // Change this to the correct path

Usage

Include the class and pass the required parameters, (*4)

require_once 'src/Kosinix/Paginator.php';

$total = 23; // This will come from your app. Eg. do an SQL count: 'SELECT COUNT(*) AS `total` FROM user'
$current_page = 2; // This will come from your app. Eg. $current_page = $_GET['page'];
$per_page = 10; // This will also come from your app. 

$paginator = new \Kosinix\Paginator($total, $current_page, $per_page);

$sql = sprintf('SELECT * FROM users LIMIT %d,%d', $paginator->getStartIndex(), $paginator->getPerPage());

// Run sql query here

The constructor accepts the following parameters:, (*5)

  • total - The total number of records.
  • current_page - The current page to display. Defaults to 1.
  • per_page - The number of records in a page. Defaults to 10.

Terms are best explained by this image, (*6)

alt tag, (*7)

Silex Service Provider

You can also create a service provider for paginator for use in Silex:, (*8)

// PaginatorServiceProvider.php

use Silex\Application;
use Silex\ServiceProviderInterface;
use Kosinix\Paginator;

class PaginatorServiceProvider implements ServiceProviderInterface {

    public function register(Application $app) {
        $app['paginator.per_page'] = isset($app['paginator.per_page']) ? (int)$app['paginator.per_page'] : 10;
        $app['paginator'] = $app->protect(
            function ($total, $page, $per_page=null) use ($app) {
                if(null === $per_page){
                    $per_page = $app['paginator.per_page'];
                }
                return new Paginator($total, $page, $per_page);
            }
        );
    }

    public function boot(Application $app) {

    }
}

Then in your app:, (*9)

// Paginator
$app->register(new PaginatorServiceProvider());

In your controller:, (*10)

$sql = 'SELECT COUNT(*) AS `total` FROM product';
$count = $app['db']->fetchAssoc($sql);
$count = (int) $count['total'];

/** @var \Kosinix\Paginator $paginator */
$paginator =  $app['paginator']($count, $page); // $page would come from your web app

Test

  • Go to the project folder and run phpunit in the command line.
  • You need to have phpunit installed globally.

License

  • MIT

The Versions

04/11 2016

dev-master

9999999-dev https://github.com/kosinix/paginator

A generic PHP class to split large data into smaller chunks for use in web apps

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

paginator

04/11 2016

3.0.0

3.0.0.0 https://github.com/kosinix/paginator

A generic PHP class to split large data into smaller chunks for use in web apps

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

paginator

26/05 2015

2.0.0

2.0.0.0 https://github.com/kosinix/paginator

A generic PHP class to split large data into smaller chunks for use in web apps

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

paginator

16/10 2014

1.0.0

1.0.0.0

  Sources   Download