One paging navigation class

Source: Internet
Author: User
? Php + versions + | PHPVersion4 | + ---------------------------------------------------------------------- + | Copyright (c) 1997-2002ThePHPGroup | + ------------------------ // + ------------------------------------------------------------------------ +
// | PHP Version 4 |
// + ------------------------------------------------------------------------ +
// | Copyright (c) 1997-2002 The PHP Group |
// + ------------------------------------------------------------------------ +
// | This source file is subject to version 2.02 of the PHP license, |
// | That is bundled with this package in the file LICENSE, and is |
// | Available at through the world-wide-web at |
// | Http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | Obtain it through the world-wide-web, please send a note to |
// License@php.net so we can mail you a copy immediately. |
// + ------------------------------------------------------------------------ +
// | Authors: Richard Heyes |
// + ------------------------------------------------------------------------ +

/**
* Pager class
*
* Handles paging a set of data. For usage see the example. php provided.
*
*/

Class Pager {

/**
* Current page
* @ Var integer
*/
Var $ _ currentPage;

/**
* Items per page
* @ Var integer
*/
Var $ _ perPage;

/**
* Total number of pages
* @ Var integer
*/
Var $ _ totalPages;

/**
* Item data. Numerically indexed array...
* @ Var array
*/
Var $ _ itemData;

/**
* Total number of items in the data
* @ Var integer
*/
Var $ _ totalItems;

/**
* Page data generated by this class
* @ Var array
*/
Var $ _ pageData;

/**
* Constructor
*
* Sets up the object and calculates the total number of items.
*
* @ Param $ params An associative array of parameters This can contain:
* CurrentPage Current Page number (optional)
* PerPage Items per page (optional)
* ItemData Data to page
*/
Function pager ($ params = array ())
{
Global $ HTTP_GET_VARS;

$ This-> _ currentPage = max (int) @ $ HTTP_GET_VARS ['pageid'], 1 );
$ This-> _ perPage = 8;
$ This-> _ itemData = array ();

Foreach ($ params as $ name => $ value ){
$ This-> {'_'. $ name} = $ value;
}

$ This-> _ totalItems = count ($ this-> _ itemData );
}

/**
* Returns an array of current pages data
*
* @ Param $ pageID Desired page ID (optional)
* @ Return array Page data
*/
Function getPageData ($ pageID = null)
{
If (isset ($ pageID )){
If (! Empty ($ this-> _ pageData [$ pageID]) {
Return $ this-> _ pageData [$ pageID];
} Else {
Return FALSE;
}
}

If (! Isset ($ this-> _ pageData )){
$ This-> _ generatePageData ();
}

Return $ this-> getPageData ($ this-> _ currentPage );
}

/**
* Returns pageID for given offset
*
* @ Param $ index Offset to get pageID
* @ Return int PageID for given offset
*/
Function getPageIdByOffset ($ index)
{
If (! Isset ($ this-> _ pageData )){
$ This-> _ generatePageData ();
}

If ($ index % $ this-> _ perPage)> 0 ){
$ PageID = ceil (float) $ index/(float) $ this-> _ perPage );
} Else {
$ PageID = $ index/$ this-> _ perPage;
}

Return $ pageID;
}

/**
* Returns offsets for given pageID. Eg, if you
* Pass it pageID one and your perPage limit is 10
* It will return you 1 and 10. PageID of 2 wowould
* Give you 11 and 20.
*
* @ Params pageID PageID to get offsets
* @ Return array First and last offsets
*/
Function getOffsetByPageId ($ pageid = null)
{
$ Pageid = isset ($ pageid )? $ Pageid: $ this-> _ currentPage;
If (! Isset ($ this-> _ pageData )){
$ This-> _ generatePageData ();
}

If (isset ($ this-> _ pageData [$ pageid]) {
Return array ($ this-> _ perPage * ($ pageid-1) + 1, min ($ this-> _ totalItems, $ this-> _ perPage * $ pageid ));
} Else {
Return array (0, 0 );
}
}

/**
* Returns back/next and page links
*
* @ Param $ back_html HTML to put inside the back link
* @ Param $ next_html HTML to put inside the next link
* @ Return array Back/pages/next links
*/
Function getLinks ($ back_html = '<back', $ next_html = 'next> ')
{
$ Url = $ this-> _ getLinksUrl ();
$ Back = $ this-> _ getBackLink ($ url, $ back_html );
$ Pages = $ this-> _ getPageLinks ($ url );
$ Next = $ this-> _ getNextLink ($ url, $ next_html );

Return array ($ back, $ pages, $ next, 'back' => $ back, 'Pages '=> $ pages, 'next' => $ next );
}

/**
* Returns number of pages
*
* @ Return int Number of pages
*/
Function numPages ()
{
Return $ this-> _ totalPages;
}

/**
* Returns whether current page is first page
*
* @ Return bool First page or not
*/
Function isFirstPage ()
{
Return ($ this-> _ currentPage = 1 );
}

/**
* Returns whether current page is last page
*
* @ Return bool Last page or not
*/
Function isLastPage ()
{
Return ($ this-> _ currentPage = $ this-> _ totalPages );
}

/**
* Returns whether last page is complete
*
* @ Return bool Last age complete or not
*/
Function isLastPageComplete ()
{
Return! ($ This-> _ totalItems % $ this-> _ perPage );
}

/**
* Calculates all page data
*/
Function _ generatePageData ()
{
$ This-> _ totalItems = count ($ this-> _ itemData );
$ This-> _ totalPages = ceil (float) $ this-> _ totalItems/(float) $ this-> _ perPage );
$ I = 1;
If (! Empty ($ this-> _ itemData )){
Foreach ($ this-> _ itemData as $ value ){
$ This-> _ pageData [$ I] [] = $ value;
If (count ($ this-> _ pageData [$ I]) >=$ this-> _ perPage ){
$ I ++;
}
}
} Else {
$ This-> _ pageData = array ();
}
}

/**
* Returns the correct link for the back/pages/next links
*
* @ Return string Url
*/
Function _ getLinksUrl ()
{
Global $ HTTP_SERVER_VARS;

// Sort out query string to prevent messy urls
$ Querystring = array ();
If (! Empty ($ HTTP_SERVER_VARS ['query _ string']) {
$ Qs = explode ('&', $ HTTP_SERVER_VARS ['query _ string']);
For ($ I = 0, $ cnt = count ($ qs); $ I <$ cnt; $ I ++ ){
List ($ name, $ value) = explode ('=', $ qs [$ I]);
If ($ name! = 'Pageid '){
$ Qs [$ name] = $ value;
}
Unset ($ qs [$ I]);
}
}
If (is_array ($ qs )){
Foreach ($ qs as $ name => $ value ){
$ Querystring [] = $ name. '='. $ value;
}
}
Return $ HTTP_SERVER_VARS ['script _ name']. '? '. Implode (' & ', $ querystring ).(! Empty ($ querystring )? '&': '). 'Pageid = ';
}

/**
* Returns back link
*
* @ Param $ url URL to use in the link
* @ Param $ link HTML to use as the link
* @ Return string The link
*/
Function _ getBackLink ($ url, $ link = '<back ')
{
// Back link
If ($ this-> _ currentPage> 1 ){
$ Back = '_ currentPage-1).' "> '. $ link .'';
} Else {
$ Back = ';
}

Return $ back;
}

/**
* Returns pages link
*
* @ Param $ url URL to use in the link
* @ Return string Links
*/
Function _ getPageLinks ($ url)
{
// Create the range
$ Params ['itemdata'] = range (1, max (1, $ this-> _ totalPages ));
$ Pager = & new Pager ($ params );
$ Links = $ pager-> getPageData ($ pager-> getPageIdByOffset ($ this-> _ currentPage ));

For ($ I = 0; $ I If ($ links [$ I]! = $ This-> _ currentPage ){
$ Links [$ I] = '_ getLinksUrl (). $ links [$ I].' "> '. $ links [$ I].'';
}
}

Return implode ('', $ links );
}

/**
* Returns next link
*
* @ Param $ url URL to use in the link
* @ Param $ link HTML to use as the link
* @ Return string The link
*/
Function _ getNextLink ($ url, $ link = 'next> ')
{
If ($ this-> _ currentPage <$ this-> _ totalPages ){
$ Next = '_ currentPage + 1).' "> '. $ link .'';
} Else {
$ Next = ';
}

Return $ next;
}
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.