Code Monkey home page Code Monkey logo

php-paginator's Introduction

PHP Paginator

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The "first" and "last" page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

Screenshots

These examples show how the paginator handles overflow when there are a lot of pages. They're rendered using the sample templates provided in the examples directory, which depend on Twitter Bootstrap. You can easily use your own custom HTML to render the pagination control instead.

Default template:




Small template (useful for mobile interfaces):




The small template renders the page number as a select list to save space:

Installation

Install with composer:

composer require "jasongrimes/paginator:~1.0"

Basic usage

Here's a quick example using the defaults:

<?php

require '../vendor/autoload.php';

use JasonGrimes\Paginator;

$totalItems = 1000;
$itemsPerPage = 50;
$currentPage = 8;
$urlPattern = '/foo/page/(:num)';

$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

?>
<html>
  <head>
    <!-- The default, built-in template supports the Twitter Bootstrap pagination styles. -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  </head>
  <body>

    <?php 
      // Example of rendering the pagination control with the built-in template.
      // See below for information about using other templates or custom rendering.

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

This will output the following:

<ul class="pagination">
  <li><a href="/foo/page/7">&laquo; Previous</a></li>
  <li><a href="/foo/page/1">1</a></li>
  <li class="disabled"><span>...</span></li>
  <li><a href="/foo/page/5">5</a></li>
  <li><a href="/foo/page/6">6</a></li>
  <li><a href="/foo/page/7">7</a></li>
  <li class="active"><a href="/foo/page/8">8</a></li>
  <li><a href="/foo/page/9">9</a></li>
  <li><a href="/foo/page/10">10</a></li>
  <li><a href="/foo/page/11">11</a></li>
  <li><a href="/foo/page/12">12</a></li>
  <li class="disabled"><span>...</span></li>
  <li><a href="/foo/page/20">20</a></li>
  <li><a href="/foo/page/9">Next &raquo;</a></li>
</ul>

To render it with one of the other example templates, just make sure the variable is named $paginator and then include the template file:

$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

include '../vendor/jasongrimes/paginator/examples/pagerSmall.phtml';


If the example templates don't suit you, you can iterate over the paginated data to render your own pagination control.

Rendering a custom pagination control

Use $paginator->getPages(), $paginator->getNextUrl(), and $paginator->getPrevUrl() to render a pagination control with your own HTML. For example:

<ul class="pagination">
    <?php if ($paginator->getPrevUrl()): ?>
        <li><a href="<?php echo $paginator->getPrevUrl(); ?>">&laquo; Previous</a></li>
    <?php endif; ?>

    <?php foreach ($paginator->getPages() as $page): ?>
        <?php if ($page['url']): ?>
            <li <?php echo $page['isCurrent'] ? 'class="active"' : ''; ?>>
                <a href="<?php echo $page['url']; ?>"><?php echo $page['num']; ?></a>
            </li>
        <?php else: ?>
            <li class="disabled"><span><?php echo $page['num']; ?></span></li>
        <?php endif; ?>
    <?php endforeach; ?>

    <?php if ($paginator->getNextUrl()): ?>
        <li><a href="<?php echo $paginator->getNextUrl(); ?>">Next &raquo;</a></li>
    <?php endif; ?>
</ul>

<p>
    <?php echo $paginator->getTotalItems(); ?> found.
    
    Showing 
    <?php echo $paginator->getCurrentPageFirstItem(); ?> 
    - 
    <?php echo $paginator->getCurrentPageLastItem(); ?>.
</p>

See the examples directory for more sample templates.

Pages data structure

$paginator->getPages();

getPages() returns a data structure like the following:

array ( 
    array ('num' => 1,     'url' => '/foo/page/1',  'isCurrent' => false),
    array ('num' => '...', 'url' => NULL,           'isCurrent' => false),
    array ('num' => 5,     'url' => '/foo/page/5',  'isCurrent' => false),
    array ('num' => 6,     'url' => '/foo/page/6',  'isCurrent' => false),
    array ('num' => 7,     'url' => '/foo/page/7',  'isCurrent' => false),
    array ('num' => 8,     'url' => '/foo/page/8',  'isCurrent' => true),
    array ('num' => 9,     'url' => '/foo/page/9',  'isCurrent' => false),
    array ('num' => 10,    'url' => '/foo/page/10', 'isCurrent' => false),
    array ('num' => 11,    'url' => '/foo/page/11', 'isCurrent' => false),
    array ('num' => 12,    'url' => '/foo/page/12', 'isCurrent' => false),
    array ('num' => '...', 'url' => NULL,           'isCurrent' => false),
    array ('num' => 20,    'url' => '/foo/page/20', 'isCurrent' => false),
)

Customizing the number of pages shown

By default, no more than 10 pages are shown, including the first and last page, with the overflow replaced by ellipses. To change the default number of pages:

$paginator->setMaxPagesToShow(5);

php-paginator's People

Contributors

cinu avatar jasongrimes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-paginator's Issues

Configurable Next and Previous text

At the moment, the $previousText and $nextText variables are hard coded in the library.
I think it would be a useful feature to have these configurable, for example for supporting translation.
Would you consider adding this to the library?

Regards,

Bootstrap 4 compatibility

I saw there's a pull request that failed, would like to revisit this. Basically just need a couple new classes. #15 Thanks!

build pages for toHtmlSemanticUI

Ao trabalhar com SemanticUI o retorno fica quebrado.
Então resolvi dar um extends na class e criar um método toHtmlSemanticUI e no __toString() fazer uma verificação.

/**

  • Render an HTML pagination control.
    *

  • @return string
    */
    public function toHtmlSemanticUI(){

    if ($this->paginator->getNumPages() <= 1) {
    return '';
    }

    $html = '

    ';

    if ($this->paginator->getPrevUrl()):
    $html .= '';
    endif;

    foreach ($this->paginator->getPages() as $page):
    if ($page['url']):
    $isCurrent = $page['isCurrent'] ? ' active "' : '';
    $html .= ''.$page['num'].'';
    else:
    $html .= "

    ".$page['num']."
    ";
    endif;
    endforeach;

    if($this->paginator->getNextUrl()):
    $html .= "";
    endif;

    $html .= '

    ';

    return $html;
    }

    public function __toString(){

    if($this->framework == 'SemanticUI'){
    return $this->toHtmlSemanticUI();
    }

    return $this->paginator->toHtml();
    }

Suppress link for current page in full width display

In the full width display, the current page button needs to have it's link suppressed.

/src/JasonGrimes/Paginator.php Line 289 needs to be changed from

                $html .= '<li' . ($page['isCurrent'] ? ' class="active"' : '') . '><a href="' . htmlspecialchars($page['url']) . '">' . htmlspecialchars($page['num']) . '</a></li>';

to:

                $html .= '<li' . ($page['isCurrent'] ? ' class="active"><a href="#' : '><a href="' . htmlspecialchars($page['url'])) . '">' . htmlspecialchars($page['num']) . '</a></li>';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.