dbCF.de

PHP class for pagination

Need a demonstration?

Some possible combinations are shown on the demo page.

How to use it?

Simply copy the code into a file (or download it here). I will call it pagination.php. Your code should look like this:

<?php
// Include file with code
include_once('pagination.php');

// Get current page
$active_page = !empty($_GET['page']) ? $_GET['page'] : 0;

// Get count of all entries
$result=mysql_query('SELECT COUNT(field) FROM table');
list($entries)=mysql_fetch_row($result);


// Create object, deliver current page and count of all entries
$pagination=new blaettern($active_page, $entries);
// Define url for links
// You could define this within my code and save this call
$pagination->setLinkHref($_SERVER['PHP_SELF'].'?page=');


// Create first list of links and show
echo $pagination->create();


// Do request with limitations (LIMIT)
$result=mysql_query('SELECT field
                     FROM tabelle
                     LIMIT '.($pagination->getEntriesPerPage() * $pagination->getActivePage()).', '.$pagination->getEntriesPerPage());
// Show found data
if (mysql_num_rows($result)>0) {
    while (list($field)=mysql_fetch_row($result)) {
        echo "$field<br />\n";
    }
}
else {
    echo "No data found!\n";
}


// Create second list of links and show
echo $pagination->create();
?>

You should get a list of links which will look similar like this (depends on count of entries and your settings): [1], >2<, >3< ... >32<, >33<, >34< - Next -- Last

What to do with it?

After clicking one of these links you will have a new $active_page. With this number you can e.g. calculate in MySQL a range of results to show (see LIMIT '.($pagination->getEntriesPerPage() * $pagination->getActivePage()).', '.$pagination->getEntriesPerPage()). So you have split the content of a page into many pages in an very easy way.

<?php
/** Function for pagination
* Function that creates a list of links which can be used to browse through a specified count of results
*
* @author Carsten Franke <mail@dbCF.de>
* @link http://dbCF.de/
* @version 1.2.1
* @copyright GPL
*/

// Only used while developing
// error_reporting(E_ALL);

/*
All possible versions
First — Previous – 1, 2, 3 … 6, 7, 8 … 11, 12, 13 – Next — Last
Previous – 1, 2, 3 … 6, 7, 8 … 11, 12, 13 – Next
First – 1, 2, 3 … 6, 7, 8 … 11, 12, 13 – Last
1, 2, 3 … 6, 7, 8 … 11, 12, 13

First — Previous – … 6, 7, 8 … – Next — Last
Previous … 6, 7, 8 … Next
First … 6, 7, 8 … Last
… 6, 7, 8 …

First — Previous – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 – Next — Last
Previous – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 – Next
First – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 – Last
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

Variables for links
Variable Example Output example
$delimiter1 — First — Previous
$delimiter2 – Previous – 1
$delimiter3 , 1, 2, 3
$delimiter4 … 1, 2, 3 … 6, 7, 8

$linkActiveText Page Page 1, Page 2, Page 3
$linkActiveBracketLeft [ 6, [7], 8
$linkActiveBracketRight ] 6, [7], 8
$linkActiveMore style=’font-size:1.2em;’ <a href=’…’ style=’font-size:1.2em;’>

$linkText Page Page 1, Page 2, Page 3
$linkBracketLeft > >1<, >2<, >3<
$linkBracketRight < >1<, >2<, >3<
$linkMore target=’_blank’ <a href=’…’ target=’_blank’>

$linkFirstText First First — Previous
$linkLastText Last Next — Last
$linkFirstLastMore style=’font-family:arial;’ <a href=’…’ style=’font-family:arial;’>

$linkNextText Next Next — Last
$linkPrevText Previous First — Previous
$linkNextPrevMore class=’navigation’ <a href=’…’ class=’navigation’>

$showCount 3 6, 7, 8
$showContinuous true/false true: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
false: depends on $showOuter
$showOuter true/false true: 1, 2, 3 … 6, 7, 8 … 11, 12, 13
false: … 6, 7, 8 …
$showNextPrev true/false true: show Next/Previous, false: don’t show
$showFirstLast true/false true: show First/Last, false: don’t show
$showActiveLink true/false true: 6, [7], 8, false: 6, 8
$showSingleLink true/false true: [1], false: (nothing)

$linkHref news.php?id=$id&page= <a href=’news.php?id=BMW&page=2’>
*/

class Pagination {
/** Creates an object for this class
*
* @access public
* @param (int) $activePage Number of active page; you get it approximately in this way: “$activePage=isset($_GET[‘ap’]) ? $_GET[‘ap’] : 0;”
* @param (int) $entries Count of all results; you get it approximately in this way: “$entries=mysql_num_rows($resource);”
*/
public function __construct($activePage, $entries) {
$this->setActivePage($activePage);
$this->setEntries($entries);
}

/** Creates list of links
*
* @access public
* @return (mixed) Returns generated list of link or false on error
*/
public function create() {
// I’m to lazy to use the long notation so I create some references… :)
$activePage = &$this->activePage;
$entries = &$this->entries;
$entriesPerPage = &$this->entriesPerPage;
$linkCount = &$this->linkCount;
$linkCountSurround = &$this->linkCountSurround;
$showCount = &$this->showCount;
$showContinuous = &$this->showContinuous;
$showOuter = &$this->showOuter;
$showSingleLink = &$this->showSingleLink;
$showActiveLink = &$this->showActiveLink;
$pageNext = &$this->pageNext;
$pagePrev = &$this->pagePrev;
$pageFirst = &$this->pageFirst;
$pageLast = &$this->pageLast;
$links = &$this->links;

// Re-initiate array
$links = array ();

// Calculate count of all possible links
$linkCount = $entries % $entriesPerPage == 0 ? (int)$entries / $entriesPerPage : (int)ceil($entries / $entriesPerPage);

// Calculate how many link will surround the active one
$linkCountSurround = floor($showCount / 2);

// If there will be only one link and the user won’t see it then return empty string
if (($showSingleLink === false || $showActiveLink === false) && $entries <= $showCount) {
$links = ”;
return $links;
}
else if ($showSingleLink === true && $showActiveLink === true && $entries <= $showCount) {
$linkCount = 1;
}

// If it makes sense ($showOuter===true but $linkCount<=$showCount*3) then overwrite $showContinuous
if ($showOuter === true && $linkCount <= $showCount * 3) {
$showContinuous = true;
}

// Check value of active page, reset if needed
if ($linkCount < $activePage) {
$activePage = $linkCount – 1;
} elseif (!is_numeric($activePage) || $activePage < 0) {
$activePage = 0;
}

// Now we calculate number for pages first, last, next and previous
// If one of these pages doesn’t exist the value will be null (not 0!)
$pageNext = $activePage + 1;

if ($pageNext >= $linkCount – 1 && $activePage == $linkCount – 1) {
$pageNext = null;
}
$pagePrev = $activePage – 1;

if ($pagePrev <= 0 && $activePage == 0) {
$pagePrev = null;
}
$pageFirst = 0;

if ($activePage == 0) {
$pageFirst = null;
}
$pageLast = $linkCount – 1;

if ($activePage == $linkCount – 1) {
$pageLast = null;
}

// Iterate through all pages (not give a damn about they will be shown at the end) and store number of page in array
for ($i = 0; $i < $linkCount; $i++) {
$links[] = $i;
}
// Create links from page numbers
$this->_makeLinks();

// If all pages should be show…
// …or count of links is lower than count of links to show, so join links with delimter3 to string
if ($showContinuous === true || $linkCount <= $showCount) {
$this->_addDelimiter3();
} else {
// Or if not all links should be shown…
// …and not the outer
if ($showOuter === false) {
// If active link is one of the outer one, then show them separate
if ($activePage <= $linkCountSurround) {
$links = $this->_getOuterLinks(‘left’);
$this->_addDelimiter3();
$this->_addDelimiter4(false, true);
} elseif ($activePage >= $pageLast – $linkCountSurround) {
$links = $this->_getOuterLinks(‘right’);

$this->_addDelimiter3();
$this->_addDelimiter4(true, false);
}
// Otherwise get the $showCount of relevant links
else {
$links = $this->_getInnerLinks();
$this->_addDelimiter3();
$this->_addDelimiter4(true, true);
}
}
// Or if not all links should be shown…
// …and the outer too
elseif ($showOuter === true) {
// Fetch all possible links
$temp[0] = $this->_getOuterLinks(‘left’);
$temp[1] = $this->_getInnerLinks();
$temp[2] = $this->_getOuterLinks(‘right’);

// Merge all three arrays into one
// No use of array_*-functions cause they destroy the index
$temp2 = $temp[0];

for ($i = 1; $i < 3; $i++) {
foreach ($temp[$i] as $k => $v) {
$temp2[$k] = $v;
}
}
$temp = $temp2;
unset ($temp2);
ksort ($temp);

// Detect successional links (distance to next link is 1) and push into an array
$last_index = 0;
$j = 0;

foreach ($temp as $k => $v) {
if ($k – $last_index > 1) {
$j++;
}

$temp2[$j][] = $v;

$last_index = $k;
}

// Join this new array to string
// No use of functions, cause they would use $this->links
unset ($temp);

for ($i = 0; $i < count($temp2); $i++) {
$temp[] = join($this->delimiter3, $temp2[$i]);
}
$links = join($this->delimiter4, $temp);
}
}

// Add links for Next/Previous and First/Last
$this->_addNextPrev();
$this->_addFirstLast();

return $links;
}

/** Generates a string of the final links from saved page-ids in $links
*
* @access private
*/
private function _makeLinks() {
// I’m to lazy to use the long notation so I create some references… :)
$activePage = &$this->activePage;
$linkHref = &$this->linkHref;
$linkActiveBracketLeft = &$this->linkActiveBracketLeft;
$linkActiveBracketRight = &$this->linkActiveBracketRight;
$linkActiveMore = &$this->linkActiveMore;
$linkActiveText = &$this->linkActiveText;
$linkBracketLeft = &$this->linkBracketLeft;
$linkBracketRight = &$this->linkBracketRight;
$linkMore = &$this->linkMore;
$linkText = &$this->linkText;
$showActiveLink = &$this->showActiveLink;
$links = &$this->links;

for ($i = 0; $i < count($links); $i++) {
$num_link = $i + $this->numberFirstPage;
$num_show = $i + 1;

if ($i == $activePage) {
$links[$i]
= “<a href='{$linkHref}{$num_link}'{$linkActiveMore}>{$linkActiveBracketLeft}{$linkActiveText}{$num_show}{$linkActiveBracketRight}</a>”;
} else {
$links[$i]
= “<a href='{$linkHref}{$num_link}'{$linkMore}>{$linkBracketLeft}{$linkText}{$num_show}{$linkBracketRight}</a>”;
}
}

// Now we hide the link for active page – if requested
if ($showActiveLink === false) {
unset($links[$activePage]);
}
unset ($temp);

foreach ($links as $k => $v) {
$temp[] = $v;
}
$links = $temp;
}

/** Returns array of inner links
*
* @access private
*/
private function _getInnerLinks() {
// I’m to lazy to use the long notation so I create some references… :)
$activePage = &$this->activePage;
$pageFirst = &$this->pageFirst;
$pageLast = &$this->pageLast;
$linkCountSurround = &$this->linkCountSurround;
$linkCount = &$this->linkCount;
$showCount = &$this->showCount;
$links = &$this->links;

$temp = false;
$from = $activePage – $linkCountSurround;

if ($from < 0) {
$from = 0;
}
$to = $from + $showCount;

if ($to > $linkCount – 1) {
$to = $linkCount – 1;
}

for ($i = $from; $i < $to; $i++) {
$temp[$i] = $links[$i];
}

return $temp;
}

/** Returns array of outer links (use ‘left’ or ‘right’)
*
* @access private
*/
private function _getOuterLinks($side = null) {
// I’m to lazy to use the long notation so I create some references… :)
$showActiveLink = &$this->showActiveLink;
$linkCount = &$this->linkCount;
$showCount = &$this->showCount;
$links = &$this->links;

$temp = false;

if ($side === ‘left’) {
for ($i = 0; $i < $showCount; $i++) {
$temp[$i] = $links[$i];
}
} elseif ($side === ‘right’) {
// The first workaround, which prevents that a link under wished number will be shown
if ($showActiveLink === false) {
$from = $linkCount – $showCount – 1;
} else {
$from = $linkCount – $showCount;
}
$to = $from + $showCount;

for ($i = $from; $i < $to; $i++) {
$temp[$i] = $links[$i];
}
}

return $temp;
}

/** Joins array to string with delimiter (see examples)
*
* @access private
*/
private function _addDelimiter1() {
if ($this->pageFirst !== null) {
$this->links = $this->delimiter1 . $this->links;
}

if ($this->pageLast !== null) {
$this->links = $this->links . $this->delimiter1;
}
}

/** Joins array to string with delimiter (see examples)
*
* @access private
*/
private function _addDelimiter2() {
// I’m to lazy to use the long notation so I create some references… :)
$links = &$this->links;
$pagePrev = &$this->pagePrev;
$pageNext = &$this->pageNext;
$delimiter2 = &$this->delimiter2;
$delimiter4 = &$this->delimiter4;

if ($pagePrev !== null &&
// Prevent that delimiter2 will be shown if delimiter4 will be too
substr($links, 0, strlen($delimiter4)) != $delimiter4) {
$links = $delimiter2 . $links;
}

if ($pageNext !== null && substr($links, -1 * strlen($delimiter4)) != $delimiter4) {
$links = $links . $delimiter2;
}
}

/** Joins array to string with delimiter (see examples)
*
* @access private
*/
private function _addDelimiter3() { $this->links = join($this->delimiter3, $this->links); }

/** Joins array to string with delimiter (see examples)
*
* @access private
*/
private function _addDelimiter4($left = false, $right = false) {
if ($left === true) {
$this->links = $this->delimiter4 . $this->links;
}

if ($right === true) {
$this->links = $this->links . $this->delimiter4;
}
}

/** Adds links Next/Previous to list – if requested
*
* @access private
*/
private function _addNextPrev() {
// I’m to lazy to use the long notation so I create some references… :)
$activePage = &$this->activePage;
$linkHref = &$this->linkHref;
$linkNextText = &$this->linkNextText;
$linkPrevText = &$this->linkPrevText;
$linkNextPrevMore = &$this->linkNextPrevMore;
$showNextPrev = &$this->showNextPrev;
$pageNext = &$this->pageNext;
$pagePrev = &$this->pagePrev;
$links = &$this->links;

if ($showNextPrev === true) {
$this->_addDelimiter2();

$temp1 = $temp2 = ”;

if ($pagePrev !== null) {
$temp1 = “<a href='{$linkHref}” . ($pagePrev – $this->stepsNextPrev + 1 + $this->numberFirstPage)
. “‘{$linkNextPrevMore}>$linkPrevText</a>”;
}

if ($pageNext !== null) {
$temp2 = “<a href='{$linkHref}” . ($pageNext + $this->stepsNextPrev – 1 + $this->numberFirstPage)
. “‘{$linkNextPrevMore}>$linkNextText</a>”;
}
$links = $temp1 . $links . $temp2;
}
}

/** Adds links First/Last to list – if requested
*
* @access private
*/
private function _addFirstLast() {
// I’m to lazy to use the long notation so I create some references… :)
$linkHref = &$this->linkHref;
$linkFirstText = &$this->linkFirstText;
$linkLastText = &$this->linkLastText;
$linkFirstLastMore = &$this->linkFirstLastMore;
$showFirstLast = &$this->showFirstLast;
$pageFirst = &$this->pageFirst;
$pageLast = &$this->pageLast;
$links = &$this->links;

if ($showFirstLast === true) {
$this->_addDelimiter1();

$temp1 = $temp2 = ”;

if ($pageFirst !== null) {
$temp1 = “<a href='{$linkHref}” . ($pageFirst + $this->numberFirstPage)
. “‘{$linkFirstLastMore}>$linkFirstText</a>”;
}

if ($pageLast !== null) {
$temp2 = “<a href='{$linkHref}” . ($pageLast + $this->numberFirstPage)
. “‘{$linkFirstLastMore}>$linkLastText</a>”;
}
$links = $temp1 . $links . $temp2;
}
}

/** Sets value of active page
* <b>ATTENTION</b> 10 here, means page 11 in output
* This value MUST be set dynamic
* ‘ap’ MUST be the same as the last variable in $linkHref (ap = active page)
*
* @access public
* @param (int) $activePage Number of active page; you get it approximately in this way: “$activePage=isset($_GET[‘ap’]) ? $_GET[‘ap’] : 0;”
* @return (bool) Returns false if $activePage has no valid value
*/
public function setActivePage($activePage) {
if (is_numeric($activePage)) {
$this->activePage = round($activePage) – $this->numberFirstPage;
return true;
} else {
return false;
}
}

/** Sets value for link of the first page
* If your first page should be linked with the value 12
* you have to enter it here
*
* @access public
* @param (int) $numberFirstPage Number of first page
* @return (bool) Returns false if $numberFirstPage has no valid value
*/
public function setNumberFirstPage($numberFirstPage) {
if (is_numeric($numberFirstPage)) {
$this->numberFirstPage = round($numberFirstPage);
return true;
} else {
return false;
}
}

/** Sets value for total number of entries
*
* @access public
* @param (int) $entries Total count of entries; you get it approximately in this way: “$entries=mysql_num_rows($resource);”
* @return (bool) Returns false if $entries has no valid value
*/
public function setEntries($entries) {
if (is_numeric($entries)) {
$this->entries = round($entries);
return true;
} else {
return false;
}
}

/** Sets value for entries per page to show
*
* @access public
* @param (int) $entriesPerPage Total count of entries to show per page
* @return (bool) Returns false if $entriesPerPage has no valid value
*/
public function setEntriesPerPage($entriesPerPage) {
if (is_numeric($entriesPerPage) && $entriesPerPage >= 0) {
$this->entriesPerPage = round($entriesPerPage);
return true;
} else {
return false;
}
}

/** Sets value of links where to refer to
* <b>IMPORTANT</b> The variable which contains value for active page MUST be at the end of the link followed by a = !! (see examples)
* The page number will be appended at the end of the link, that’s why you must pay attention on it!
*
* @access public
* @param (string) $linkHref URL where links should refer to
* @return (bool) Returns false if $linkHref has no valid value
*/
public function setLinkHref($linkHref) {
if (is_string($linkHref)) {
if (substr($linkHref, -1) != ‘=’) {
$linkHref .= ‘=’;
}
$this->linkHref = trim($linkHref);
return true;
} else {
return false;
}
}

/** Sets delimiter between links First/Previous and Next/Last
*
* @access public
* @param (string) $delimiter1 Delimiter between links First/Previous and Next/Last (see ‘All possible versions’)
* @return (bool) Returns false if $delimiter1 has no valid value
*/
public function setDelimiter1($delimiter1 = ‘ — ‘) {
if (is_string($delimiter1)) {
$this->delimiter1 = $delimiter1;
return true;
} else {
return false;
}
}

/** Sets delimiter between link Previous and page numbers and Next and page numbers
*
* @access public
* @param (string) $delimiter2 Delimiter between link Previous and page numbers and Next and page numbers (see ‘All possible versions’)
* @return (bool) Returns false if $delimiter2 has no valid value
*/
public function setDelimiter2($delimiter2 = ‘ – ‘) {
if (is_string($delimiter2)) {
$this->delimiter2 = $delimiter2;
return true;
} else {
return false;
}
}

/** Sets delimiter between links of single pages
*
* @access public
* @param (string) $delimiter3 Delimiter between links of single pages (see ‘All possible versions’)
* @return (bool) Returns false if $delimiter3 has no valid value
*/
public function setDelimiter3($delimiter3 = ‘, ‘) {
if (is_string($delimiter3)) {
$this->delimiter3 = $delimiter3;
return true;
} else {
return false;
}
}

/** Sets delimiter between links of single pages
*
* @access public
* @param (string) $delimiter4 Delimiter between links of single pages (see ‘All possible versions’)
* @return (bool) Returns false if $delimiter4 has no valid value
*/
public function setDelimiter4($delimiter4 = ‘ … ‘) {
if (is_string($delimiter4)) {
$this->delimiter4 = $delimiter4;
return true;
} else {
return false;
}
}

/** Sets value of chars in front and behind active link
*
* @access public
* @param (string) $linkActiveBracketLeft Chars in front of active link (see ‘Variables for links’)
* @param (string) $linkActiveBracketRight Chars behind of active link (see ‘Variables for links’)
* @return (bool) Returns false if $linkActiveBracketLeft or $linkActiveBracketRight has no valid value
*/
public function setLinkActiveBracket($linkActiveBracketLeft = ‘[‘, $linkActiveBracketRight = ‘]’) {
if (is_string($linkActiveBracketLeft) && is_string($linkActiveBracketRight)) {
$this->linkActiveBracketLeft = trim($linkActiveBracketLeft);
$this->linkActiveBracketRight = trim($linkActiveBracketRight);
return true;
} else {
return false;
}
}

/** Sets enhanced user adjustments to active link
*
* @access public
* @param (string) $linkActiveMore Enhanced user adjustments to active link (JavaScript, CSS, target, …)
* @return (bool) Returns false if $linkActiveMore has no valid value
*/
public function setLinkActiveMore($linkActiveMore = ”) {
if (is_string($linkActiveMore)) {
$linkActiveMore = ‘ ‘ . trim($linkActiveMore);
$this->linkActiveMore = $linkActiveMore;
return true;
} else {
return false;
}
}

/** Sets value of chars in front and behind of every (not active) link
*
* @access public
* @param (string) $linkBracketLeft Chars in front of every (not active) link (see ‘Variables for links’)
* @param (string) $linkBracketRight Chars behind every (not active) link (see ‘Variables for links’)
* @return (bool) Returns false if $linkBracketLeft or $linkBracketRight has no valid value
*/
public function setLinkBracket($linkBracketLeft = ”, $linkBracketRight = ”) {
if (is_string($linkBracketLeft) && is_string($linkBracketRight)) {
$this->linkBracketLeft = trim($linkBracketLeft);
$this->linkBracketRight = trim($linkBracketRight);
return true;
} else {
return false;
}
}

/** Sets enhanced user adjustments to every (not active) link
*
* @access public
* @param (string) $linkMore Enhanced user adjustments to every (not active) link (JavaScript, CSS, target, …)
* @return (bool) Returns false if $linkMore has no valid value
*/
public function setLinkMore($linkMore = ”) {
if (is_string($linkMore)) {
$linkMore = ‘ ‘ . trim($linkMore);
$this->linkMore = $linkMore;
return true;
} else {
return false;
}
}

/** Sets text for links Previous/Next
*
* @access public
* @param (string) $linkNextText Text for link Next
* @param (string) $linkPrevText Text for link Previous
* @return (bool) Returns false if $linkNextText or $linkPrevText has no valid value
*/
public function setLinkNextPrevText($linkNextText = ‘Weiter’, $linkPrevText = ‘Zur&uuml;ck’) {
if (is_string($linkNextText) && is_string($linkPrevText)) {
$this->linkNextText = $linkNextText;
$this->linkPrevText = $linkPrevText;
return true;
} else {
return false;
}
}

/** Sets enhanced user adjustments for text for links Previous/Next
*
* @access public
* @param (string) $linkNextPrevMore Enhanced user adjustments for links Next/Previous (JavaScript, CSS, target, …)
* @return (bool) Returns false if $linkNextPrevMore has no valid value
*/
public function setLinkNextPrevMore($linkNextPrevMore = ”) {
if (is_string($linkNextPrevMore)) {
$linkNextPrevMore = ‘ ‘ . trim($linkNextPrevMore);
$this->linkNextPrevMore = $linkNextPrevMore;
return true;
} else {
return false;
}
}

/** Sets text for links First/Last
*
* @access public
* @param (string) $linkFirstText Text for link First
* @param (string) $linkLastText Text for link Last
* @return (bool) Returns false if $linkFirstText or $linkLastText has no valid value
*/
public function setLinkFirstLastText($linkFirstText = ‘Anfang’, $linkLastText = ‘Ende’) {
if (is_string($linkFirstText) && is_string($linkLastText)) {
$this->linkFirstText = $linkFirstText;
$this->linkLastText = $linkLastText;
return true;
} else {
return false;
}
}

/** Sets enhanced user adjustments for text for links First/Last
*
* @access public
* @param (string) $linkFirstLastMore Enhanced user adjustments for links First/Last (JavaScript, CSS, target, …)
* @return (bool) Returns false if $linkFirstLastMore has no valid value
*/
public function setLinkFirstLastMore($linkFirstLastMore = ”) {
if (is_string($linkFirstLastMore)) {
$linkFirstLastMore = ‘ ‘ . trim($linkFirstLastMore);
$this->linkFirstLastMore = $linkFirstLastMore;
return true;
} else {
return false;
}
}

/** Sets value of how many links to show
*
* @access public
* @param (int) $showCount Value of how many links to show (odd numbers are more ideal)
* @return (bool) Returns false if $showCount has no valid value
*/
public function setShowCount($showCount = 3) {
if (is_numeric($showCount) && $showCount >= 3) {
$this->showCount = ceil($showCount);
return true;
} else {
return false;
}
}

/** Sets value of steps to do with Next/Previous
*
* @access public
* @param (bool) $stepsNextPrev Value of steps to do with Next/Previous
* @return (bool) Returns false if $stepsNextPrev has no valid value
*/
public function setStepsNextPrev($stepsNextPrev = null) {
if (is_numeric($stepsNextPrev) && $stepsNextPrev >= 1) {
$this->stepsNextPrev = round($stepsNextPrev);
return true;
} else {
return false;
}
}

/** Sets value to show all links at once or not
*
* @access public
* @param (bool) $showContinuous Value to show all links at once (true) or not (false)
* @return (bool) Returns false if $showContinuous has no valid value
*/
public function setShowContinuous($showContinuous = false) {
if (is_bool($showContinuous)) {
$this->showContinuous = $showContinuous;
return true;
} else {
return false;
}
}

/** Sets value to show outer links or not
*
* @access public
* @param (bool) $showOuter Value to show outer links (true) or not (false) (see ‘All possible versions’)
* @return (bool) Returns false if $showOuter has no valid value
*/
public function setShowOuter($showOuter = true) {
if (is_bool($showOuter)) {
$this->showOuter = $showOuter;
return true;
} else {
return false;
}
}

/** Sets value to show active link or not
*
* @access public
* @param (bool) $showActiveLink Value to show active link (true) or not (false)
* @return (bool) Returns false if $showActiveLink has no valid value
*/
public function setShowActiveLink($showActiveLink = true) {
if (is_bool($showActiveLink)) {
$this->showActiveLink = $showActiveLink;
return true;
} else {
return false;
}
}

/** Sets value to show Previous/Next or not
*
* @access public
* @param (bool) $showNextPrev Value to show Previous/Next (true) or not (false)
* @return (bool) Returns false if $showNextPrev has no valid value
*/
public function setShowNextPrev($showNextPrev = true) {
if (is_bool($showNextPrev)) {
$this->showNextPrev = $showNextPrev;
return true;
} else {
return false;
}
}

/** Sets value to show First/Last or not
*
* @access public
* @param (bool) $showFirstLast Value to show First/Last (true) or not (false)
* @return (bool) Returns false if $showFirstLast has no valid value
*/
public function setShowFirstLast($showFirstLast = true) {
if (is_bool($showFirstLast)) {
$this->showFirstLast = $showFirstLast;
return true;
} else {
return false;
}
}

/** Sets value to show a single link or not
*
* @access public
* @param (bool) $showSingleLink Value to show a single link (true) or nothing (false)
* @return (bool) Returns false if $showSingleLink has no valid value
*/
public function setShowSingleLink($showSingleLink = false) {
if (is_bool($showSingleLink)) {
$this->showSingleLink = $showSingleLink;
return true;
} else {
return false;
}
}

/** Returns number of entries per page
*
* @access public
* @return (int) Number of links to show per page
*/
public function getEntriesPerPage() {
return $this->entriesPerPage;
}

/** Returns (possibly corrected) number of active page
*
* @access public
* @return (int) (possibly corrected) number of active page
*/
public function getActivePage() {
return $this->activePage;
}

/** Shows value of every relevant variable
*
* @access public
*/
public function dump() {
$width = 30;
$vars = array (
‘activePage’,
‘delimiter1’,
‘delimiter2’,
‘delimiter3’,
‘delimiter4’,
‘entries’,
‘entriesPerPage’,
‘linkActiveBracketLeft’,
‘linkActiveBracketRight’,
‘linkActiveMore’,
‘linkActiveText’,
‘linkBracketLeft’,
‘linkBracketRight’,
‘linkCount’,
‘linkCountSurround’,
‘linkFirstLastMore’,
‘linkFirstText’,
‘linkHref’,
‘linkLastText’,
‘linkMore’,
‘linkNextPrevMore’,
‘linkNextText’,
‘linkPrevText’,
‘linkText’,
‘pageFirst’,
‘pageLast’,
‘pageNext’,
‘pagePrev’,
‘showActiveLink’,
‘showContinuous’,
‘showCount’,
‘showFirstLast’,
‘showNextPrev’,
‘showOuter’,
‘showSingleLink’,
‘stepsNextPrev’
);

echo “<pre>\n”;

for ($i = 0; $i < count($vars); $i++) {
$temp = $this->$vars[$i];

if (is_bool($temp)) {
$temp = $temp ? ‘true’ : ‘false’;
}

echo ‘$’ . str_pad($vars[$i], $width, ‘ ‘, STR_PAD_RIGHT) . ” = $temp\n”;
}

if (is_array($this->links)) {
echo ‘$links = ‘;
print_r ($this->links);

echo ‘$links = Array’ . “\n(\n”;

for ($i = 0; $i < count($this->links); $i++) {
echo ” [$i] => ” . htmlentities($this->links[$i]) . “\n”;
}

echo “)\n”;
} else {
echo ‘$’ . str_pad(‘links’, $width, ‘ ‘, STR_PAD_RIGHT) . ” = $this->links\n”;

echo ‘$’ . str_pad(‘links’, $width, ‘ ‘, STR_PAD_RIGHT) . ‘ = ‘
. htmlentities(str_replace(‘<a href’, “\n <a href”, $this->links)) . “\n”;
}

echo “</pre>\n”;
}

// If you change any of these values below you should know what you are doing! They won’t be checked!
// So I suggest to use the existing methods.

//////////////////////////////////////////////////
// Required variables
//////////////////////////////////////////////////
/** Number of active page (page 1 has value $this->numberFirstPage)
*
* @access private
* @var (int)
*/
private $activePage = 0;

/** Number for link of first page
*
* @access private
* @var (int)
*/
private $numberFirstPage = 1;

/** Total count of entries
*
* @access private
* @var (int)
*/
private $entries = 0;

/** Entries per page to show
*
* @access private
* @var (int)
*/
private $entriesPerPage = 3;

/** URL to file, plus variable for number of page
*
* @access private
* @var (string)
*/
private $linkHref = ”;

//////////////////////////////////////////////////
// Optional variables
//////////////////////////////////////////////////
/** Delimiter, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $delimiter1 = ‘ – ‘;

/** Delimiter, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $delimiter2 = ‘ – ‘;

/** Delimiter, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $delimiter3 = ‘ – ‘;

/** Delimiter, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $delimiter4 = ‘ … ‘;

/** Text in front of active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkActiveText = ”;

/** Chars in front of active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkActiveBracketLeft = ‘[‘;

/** Chars behind active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkActiveBracketRight = ‘]’;

/** Enhanced user adjustments for active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkActiveMore = ”;

/** Text in front of every not active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkText = ”;

/** Chars in front of every not active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkBracketLeft = ”;

/** Chars behind every not active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkBracketRight = ”;

/** Enhanced user adjustments for every not active link, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkMore = ”;

/** Label for ‘Next’ (next page), see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkNextText = ‘Next’;

/** Label for ‘Previous’ (previous page), see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkPrevText = ‘Previous’;

/** Enhanced user adjustments for links ‘Next’ and ‘Previous’, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkFirstLastMore = ”;

/** Label for ‘First’ (first page), see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkFirstText = ‘First’;

/** Label for ‘Last’ (last page), see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkLastText = ‘Last’;

/** Enhanced user adjustments for links ‘First’ and ‘Last’, see ‘Variables for links’
*
* @access private
* @var (string)
*/
private $linkNextPrevMore = ”;

/** Number of links to show, see ‘Variables for links’
*
* @access private
* @var (int)
*/
private $showCount = 3;

/** Show all links true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showContinuous = false;

/** Show outer links true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showOuter = true;

/** Show ‘Next’ and ‘Previous’ true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showNextPrev = true;

/** Show ‘First’ and ‘Last’ true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showFirstLast = true;

/** Show link of active page true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showActiveLink = true;

/** Show single link true/false, see ‘Variables for links’
*
* @access private
* @var (bool)
*/
private $showSingleLink = false;

/** Count of pages to jump on ‘Next’/’Previous’
*
* @access private
* @var (bool)
*/
private $stepsNextPrev = 1;

//////////////////////////////////////////////////
// Squarely internal variables
//////////////////////////////////////////////////
/** Total count of links
*
* @access private
* @var (int)
*/
private $linkCount = 0;

/** Count of links which surround the active
*
* @access private
* @var (int)
*/
private $linkCountSurround = 0;

/** Number of previous page
*
* @access private
* @var (int)
*/
private $pagePrev = 0;

/** Number of next page
*
* @access private
* @var (int)
*/
private $pageNext = 0;

/** Number of first page
*
* @access private
* @var (int)
*/
private $pageFirst = 0;

/** Number of last page
*
* @access private
* @var (int)
*/
private $pageLast = 0;

/** Array of links and string on return
*
* @access private
* @var (mixed)
*/
private $links = array ();
}

/*
//////////////////////////////////////////////////
// A few (all?) examples
//////////////////////////////////////////////////
echo “<html><body bgcolor=’white’ style=’margin:5px; font-size:0.8em; font-family:verdana;’>\n”;
$activePage = !empty($_GET[‘page’]) ? $_GET[‘page’] : 0;
$PAGES = 54;

echo ‘all, with active and with big (5 pages) steps<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);
$pagination->setStepsNextPrev(5);

echo $pagination->create() . “<br>\n”;

echo ‘<br>3 Links – inner, with active and big (3 pages) steps<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);
$pagination->setStepsNextPrev(3);

echo $pagination->create() . “<br>\n”;

echo ‘<br>Active page and entries per page of links above<br>’;

echo ‘Active page: ‘ . $pagination->getActivePage() . ‘, Entries per page: ‘ . $pagination->getEntriesPerPage() . “<br>\n”;

echo ‘<hr>’;

echo ‘<br>all, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>all, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>all, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>all, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>all, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>all, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(true);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<hr>’;

echo ‘<br>3 Links – inner, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>3 Links – inner, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>4 Links – inner, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>4 Links – inner, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>5 Links – inner, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>5 Links – inner, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(false);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<hr>’;

echo ‘<br>3 Links – outer, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>3 Links – outer, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(3);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>4 Links – outer, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>4 Links – outer, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(4);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;

echo ‘<br>5 Links – outer, with active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(true);

echo $pagination->create() . “<br>\n”;

echo ‘<br>5 Links – outer, without active<br>’;
$pagination = new Pagination($activePage, $PAGES);
$pagination->setLinkHref($_SERVER[‘PHP_SELF’] . ‘?page=’);
$pagination->setShowCount(5);
$pagination->setShowContinuous(false);
$pagination->setShowOuter(true);
$pagination->setShowActiveLink(false);

echo $pagination->create() . “<br>\n”;
*/
?>

Posted in Dev

One thought on “PHP class for pagination

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Scroll To Top