Zebra_Pagination Zebra Pagination

Class: Zebra_Pagination

source file: /Zebra_Pagination.php

A generic, Twitter Bootstrap compatible (versions 3, 4 and 5), pagination script that automatically generates navigation links as well as next/previous page links, given the total number of records and the number of records to be shown per page.

Useful for breaking large sets of data into smaller chunks, reducing network traffic and, at the same time, improving readability, aesthetics and usability.

Read more here

Author(s):

Version:

  • 2.4.6 (last revision: January 15, 2024)
    See CHANGELOG

License:

Copyright:

  • © 2009 - 2024 Stefan Gabos

Class methods

constructor __construct()

void __construct ()

Constructor of the class.

Initializes the class and the default properties.

top

method always_show_navigation()

void always_show_navigation ( [ boolean $status = true ] )

By default, the previous page and next page links are always shown.

By disabling this feature, the previous page and next page links will only be shown if there are more pages than selectable pages.

  1. // show "previous page" / "next page" only if there are more pages
  2. // than there are selectable pages
  3. $pagination->always_show_navigation(false);
Arguments
boolean $status

(Optional) If set to FALSE, the previous page and next page links will only be shown if there are more pages than selectable pages.

Default is TRUE.

Tags
since:   2.0
top

method avoid_duplicate_content()

void avoid_duplicate_content ( [ boolean $status = true ] )

From a search engine's point of view URL https://www.mywebsite.com/list points to a different place than where https://www.mywebsite.com/list?page=1 points to (because of the added query string in the second URL), but because both have the same content, your page will get an SEO penalization.

In order to avoid this, the library will have for the first page (or last, if you are displaying links in reverse order) the same path as you have for when you are accessing the page for the first (un-paginated) time.

If you want to disable this behavior call this method with its argument set to FALSE.

  1. // don't avoid duplicate content
  2. $pagination->avoid_duplicate_content(false);
Arguments
boolean $status

(Optional) If set to FALSE, the library will have for the first page (or last, if you are displaying links in reverse order) a different path than the one you have when you are accessing the page for the first (un-paginated) time.

Default is TRUE.

Tags
since:   2.0
top

method base_url()

void base_url ( [ string $base_url = '' ] , [ boolean $preserve_query_string = true ] )

The base URL to be used when generating the navigation links.

This is helpful for the case when the URL where the records are paginated may have parameters that are not needed for subsequent requests generated by pagination.

For example, suppose some records are paginated at https://yourwebsite/mypage/. When a record from the list is updated, the URL could become something like https://youwebsite/mypage/?action=updated. Based on the value of action a message would be shown to the user.

Because of the way this script works, the pagination links would become

https://youwebsite/mypage/?action=updated&page=[page number]

when method is get and variable_name is page

https://youwebsite/mypage/page[page number]/?action=updated

when method is url and variable_name is page

As a result, whenever the user would paginate, the message would be shown to him again and again because action will be preserved in the URL!

The solution is to set the base_url to https://youwebsite/mypage/ and in this way, regardless of how the URL changes, the pagination links will always be in the form of

https://youwebsite/mypage/?page=[page number]

when method is get and variable_name is page

https://youwebsite/mypage/page[page number]/

when method is url and variable_name is page

Of course, you may still have query strings in the value of the base_url if you wish so, and these will be preserved when paginating.

If you need to preserve the hash in the URL, make sure to include the zebra_pagination.js file in your page!

Arguments
string $base_url

(Optional) The base URL to be used when generating the navigation links

Defaults is whatever returned by $_SERVER['REQUEST_URI']

boolean $preserve_query_string

(Optional) Indicates whether values in query strings, other than those set in base_url, should be preserved

Default is TRUE

top

method condensed()

void condensed ( [ boolean $extra_condensed = false ] )

Removes pagination links leaving only the next and previous buttons, links to the first and last pages, as well as a label showing the current page and the total available pages.

Setting selectable pages to a value lower than 5 will automatically turn condensed mode on.

Arguments
boolean $extra_condensed

Turning extra condensed mode on will also remove the links to the first and last pages.

Default is FALSE.

Tags
since:   2.4.0
top

method css_classes()

void css_classes ( array $css_classes )

Allows defining of custom CSS class names to be applied to the HTML markup.

Arguments
array $css_classes

An associative array with one or more or all of the following keys:

  • list, for setting the CSS class name to be used for the ordered list (<ol>)
  • list_item, for setting the CSS class name to be used for the list item (<li>)
  • anchor, for setting the CSS class name to be used for the anchor (<a>)

The default generated HTML markup looks like below:

  1. <div class="Zebra_Pagination">
  2.     <ol class="pagination">
  3.         <li class="page-item">
  4.             <a href="path/to/first/page/" class="page-link">1</a>
  5.         </li>
  6.         <li class="page-item">
  7.             <a href="path/to/second/page/" class="page-link">2</a>
  8.         </li>
  9.         ...the other pages...
  10.     </ol>
  11. </div>

Calling this method with the following argument...

  1. $pagination->css_classes(array(
  2.     'list'      =>  'foo',
  3.     'list_item' =>  'bar',
  4.     'anchor'    =>  'baz',
  5. ));

...would result in the following markup:

  1. <div class="Zebra_Pagination">
  2.     <ol class="foo">
  3.         <li class="bar">
  4.             <a href="path/to/first/page/" class="baz">1</a>
  5.         </li>
  6.         <li class="bar">
  7.             <a href="path/to/second/page/" class="baz">2</a>
  8.         </li>
  9.         ...the other pages...
  10.     </ol>
  11. </div>

Default values are:

  1. $pagination->css_classes(array(
  2.     'list'      =>  'pagination',
  3.     'list_item' =>  'page-item',
  4.     'anchor'    =>  'page-link',
  5. ));

These values make the resulting markup to be compatible with versions 3, 4 and 5 of Twitter Bootstrap.

top

method get_page()

int get_page ()

Returns the number of the currently selected page.

  1. // echoes the current page
  2. echo $pagination->get_page();
Tags
return:   Return the number of the currently selected page
top

method get_pages()

int get_pages ()

Returns the total number of available pages.

The value is computed based on the total number of records and the number of records to be shown per page.

  1. // get the total number of pages
  2. echo $pagination->get_pages();
Tags
return:   The total number of available pages
since:   2.1
top

method labels()

void labels ( [ string $previous = '&laquo;' ] , [ string $next = '&raquo;' ] , [ string $progress = '%d / %d' ] )

Change the labels for the previous page and next page links as well as the label used to indicate progress when in condensed mode.

  1. // change the default labels
  2. $pagination->labels('Previous''Next''Page %d of %d pages');
Arguments
string $previous

(Optional) The label for the previous page link.

Default is &laquo; (which looks like «)

string $next

(Optional) The label for the next page link.

Default is &raquo; (which looks like »).

string $progress

(Optional) The label for showing the current progress when in condensed mode.

Default is %d / %d

First %d will be replaced with the current page while the second one with the number of total pages.

Tags
since:   2.0
top

method method()

void method ( [ string $method = 'get' ] )

Sets the method to be used for page propagation.

  1. // set the method to the SEO friendly way
  2. $pagination->method('url');
Arguments
string $method

(Optional) The method to be used for page propagation.

Valid values are:

  • url - page propagation is done in a SEO friendly way

This method requires the mod_rewrite module to be enabled on your Apache server (or the equivalent for other web servers).

When using this method, the current page will be passed in the URL as

https://youwebsite.com/yourpage/[variable name][page number]/

where variable name is set through variable_name and page number represents the current page.

  • get - page propagation is done through GET

When using this method, the current page will be passed in the URL as

https://youwebsite.com/yourpage?[variable name]=[page number]

where variable name is set through variable_name and page number represents the current page.

Default is get.

top

method navigation_position()

void navigation_position ( string $position )

Sets the position of the next and previous page, relative to the links to individual pages.

Arguments
string $position

By default, the links for the next and previous page are shown on the outside (to the left and right) of the links to individual pages.

These links can also be shown both on the left or both on the right of the links to individual pages by setting this argument to left or right respectively.

Valid values are left, right and outside.

Default is outside.

Tags
since:   2.1
top

method padding()

void padding ( [ boolean $status = true ] )

Sets whether page numbers should be prefixed with zeros.

This is useful to keep the layout consistent by having the same number of characters for each page number.

  1. // disable padding numbers with zeros
  2. $pagination->padding(false);
Arguments
boolean $status

(Optional) Setting this property to FALSE will disable padding.

Default is TRUE.

top

method records()

void records ( integer $records )

Defines the total number of records that need to be paginated.

Based on this and on the number of records to be shown per page, the script will know how many pages there are.

  1. // tell the script that there are 100 total records
  2. $pagination->records(100);
Arguments
integer $records The total number of records that need to be paginated.
top

method records_per_page()

void records_per_page ( integer $records_per_page )

Defines the number of records that are displayed on a single page.

Based on this and on the total number of records, the script will know how many pages there are.

  1. // tell the class that there are 20 records displayed on one page
  2. $pagination->records_per_page(20);
Arguments
integer $records_per_page

The number of records displayed on a single page.

Default is 10.

top

method render()

mixed render ( [ boolean $return_output = false ] )

Generates and outputs or returns the HTML markup for the pagination.

  1. // generate output
  2. // don't echo it but return it instead
  3. $output $pagination->render(true);

If Twitter Bootstrap is not present on the page, make sure to load the default styles by including the zebra_pagination.css file.

Arguments
boolean $return_output

(Optional) Setting this argument to TRUE will instruct the script to return the generated output rather than outputting it to the screen.

Default is FALSE.

top

method reverse()

void reverse ( [ boolean $reverse = false ] )

By default, pagination links are shown in natural order, from 1 to the number of total pages.

Calling this method with the TRUE argument will generate links in reverse order, from the number of total pages down to 1.

  1. // show pagination links in reverse order rather than in natural order
  2. $pagination->reverse(true);
Arguments
boolean $reverse

(Optional) Set it to TRUE to generate navigation links in reverse order.

Default is FALSE.

Tags
since:   2.0
top

method selectable_pages()

void selectable_pages ( integer $selectable_pages )

Defines the number of pagination links to be displayed at once besides previous page and next page links.

  1. // display links to 15 pages
  2. $pagination->selectable_pages(15);
Arguments
integer $selectable_pages

The number of pagination links to be displayed at once besides previous page and next page links.

For optimal results this should be an odd value so that the number of links shown to the left and right of the current page is the same.

Setting this to a value lower than 5 will automatically turn condensed mode on.

Default is 11.

top

method set_page()

void set_page ( integer $page )

Sets the current page.

  1. // sets the fifth page as the current page
  2. $pagination->set_page(5);
Arguments
integer $page

The page's number.

A number lower than 1 will be interpreted as 1, while a number greater than the total number of pages will be interpreted as the last page.

top

method trailing_slash()

void trailing_slash ( [ boolean $status = true ] )

Enables or disables trailing slash on the generated URLs when method is url.

From an SEO perspective, a page with trailing slash is considered different than the same page without the trailing slash. Read more on the subject at Google Webmaster's official blog.

  1. // disables trailing slashes on generated URLs
  2. $pagination->trailing_slash(false);
Arguments
boolean $status

(Optional) Setting this property to FALSE will disable trailing slashes on generated URLs when method is url.

Default is TRUE (trailing slashes are enabled by default).

top

method variable_name()

void variable_name ( string $variable_name )

Sets the variable name to be used for page propagation.

  1. // sets the variable name to "foo"
  2. // now, in the URL, the current page will be passed either as
  3. // "foo=[page number]" (if method is "get") or as
  4. // "/foo[page number]" (if method is "url")
  5. $pagination->variable_name('foo');
Arguments
string $variable_name

A string representing the variable name to be used for page propagation.

Default is page.

top