Php paging programmer-PHP source code

Source: Internet
Author: User
Ec (2); classPage {& nbsp; publicparam & nbsp; public $ pageName & nbsp; page; & nbsp; default page tag, that is, filename. php? PageName & nbsp; & script ec (2); script in pageName4

Class Page {
// Public param
Public $ pageName = 'page'; // default page tag, that is, filename. php? PageName = 4
Public $ prevPage = '<'; // Previous Page
Public $ nextPage = '>'; // next page
Public $ prevBar = '<'; // The last page
Public $ nextBar = '>'; // next page
// Private param
Private $ _ totalNums = 1; // total number of records
Private $ _ barNum = 10; // Number of pagination entries displayed
Private $ _ totalPage = 0; // total number of pages
Private $ _ nowPage = 1; // current page
Private $ _ perPage = 10; // number of records displayed on each page
Private $ _ preUrl = ''; // url prefix
Private $ _ preNow = 0; // The number of pages displayed before the current page (the range should be 0 to $ _ barNum-1)
Private $ _ fromPage = 0; // the start point of the paging entry
Private $ _ toPage = 0; // The end of the page
Private $ _ offset = 0; // SQL query record offset
// Public functions -----------------------------------------------------------------
// Initiate the paging variable of the constructor
// @ Param $ nums int total number of records
// @ Param $ per int number of records per page
// @ Param $ now int current page number
// @ Param $ url string url prefix blank by default
Public function _ construct ($ nums, $ per = '', $ preNow ='', $ barNum = '', $ now ='', $ url = ''){
// Initialization
$ This-> _ set ($ nums, $ per, $ preNow, $ barNum, $ now, $ url );
$ This-> _ totalPage = ceil ($ this-> _ totalNums/$ this-> _ perPage );
$ This-> _ offset = ($ this-> _ nowPage-1) * $ this-> _ perPage;
}
// Previous Page
Public function getPrevPage (){
// The current page is not the first page
If ($ this-> _ nowPage> 1 ){
Return $ this-> _ setLink ($ this-> _ setUrl ($ this-> _ nowPage-1), $ this-> prevPage, 'prevpage ');
}
}
// Next page
Public function getNextPage (){
// The current page is smaller than the total number of pages
If ($ this-> _ nowPage <$ this-> _ totalPage ){
Return $ this-> _ setLink ($ this-> _ setUrl ($ this-> _ nowPage + 1), $ this-> nextPage, 'nextpage ');
}
}
// Page 1
Public function getFirstPage (){
// The start point is not the first page
If ($ this-> _ nowPage-$ this-> _ preNow)> 1 ){
Return $ this-> _ setLink ($ this-> _ setUrl (1), '1... ', 'firstpage ');
}
}
// Last page
Public function getLastPage (){
// The end is not equal to the total number of pages
If ($ this-> _ nowPage-$ this-> _ preNow + $ this-> _ barNum) <= $ this-> _ totalPage ){
Return $ this-> _ setLink ($ this-> _ setUrl ($ this-> _ totalPage ),'... '. $ this-> _ totalPage, 'lastpage ');
}
}
// Previous Page
Public function getPrevBar (){
// The start point is greater than the number of pages.
If ($ this-> _ nowPage-$ this-> _ preNow)> $ this-> _ barNum ){
Return $ this-> _ setLink ($ this-> _ setUrl ($ this-> _ nowPage-$ this-> _ preNow)-$ this-> _ barNum ), $ this-> prevBar, 'prevbar ');
}
}
// Next page
Public function getNextBar (){
// The end is not equal to the total number of pages
If ($ this-> _ nowPage-$ this-> _ preNow + $ this-> _ barNum) <= $ this-> _ totalPage ){
Return $ this-> _ setLink ($ this-> _ setUrl ($ this-> _ fromPage + $ this-> _ barNum), $ this-> nextBar, 'nextar ');
}
}
// Pagination bar
Public function pageBar (){
// Initialize the start and end endpoints of the paging bar
$ This-> _ toPage = $ this-> _ nowPage + ($ this-> _ barNum-$ this-> _ preNow-1 );
If ($ this-> _ toPage> $ this-> _ totalPage ){
$ This-> _ preNow = $ this-> _ barNum-($ this-> _ totalPage-$ this-> _ nowPage + 1 );
$ This-> _ toPage = $ this-> _ totalPage;
}
If ($ this-> _ toPage <$ this-> _ barNum ){
$ This-> _ toPage = $ this-> _ barNum;
}
$ This-> _ fromPage = $ this-> _ nowPage-$ this-> _ preNow;
If ($ this-> _ fromPage <1 ){
$ This-> _ fromPage = 1;
}
// Initialize the paging bar
$ Return = '';
For ($ I = $ this-> _ fromPage; $ I <= $ this-> _ toPage; $ I ++ ){
If ($ I! = $ This-> _ nowPage ){
$ Return. = $ this-> _ setLink ($ this-> _ setUrl ($ I), $ I, 'page ');
} Else {
$ Return. = ''. $ I .'';
}
}
Return $ return;
}
// Return offset for SQL query
Public function getOffset (){
Return $ this-> _ offset;
}
// The total number of returned records
Public function getTotalNums (){
Return ''. $ this-> _ totalNums .'';
}
// Display the page
Public function showPage (){
Return $ this-> getTotalNums (). $ this-> getFirstPage (). $ this-> getPrevBar (). $ this-> getPrevPage (). $ this-> pageBar (). $ this-> getNextPage (). $ this-> getNextBar (). $ this-> getLastPage ();
}
// Private functions ----------------------------------------------------------------
//
Private function _ set ($ nums, $ per, $ preNow, $ barNum, $ now, $ url ){
// Set the total number of records
If ($ nums> 0 ){
$ This-> _ totalNums = $ nums;
}
// Set the number of records displayed on each page
If ($ per> 0 ){
$ This-> _ perPage = $ per;
}
// Set the number of pages displayed before the current page
If ($ preNow> 0 ){
$ This-> _ preNow = $ preNow;
}
// Set the number of paging links
If ($ barNum> 0 ){
$ This-> _ barNum = $ barNum;
}
// Set the current page
If (empty ($ now )){
// Automatically obtain
If (isset ($ _ GET [$ this-> pageName]) {
$ This-> _ nowPage = intval ($ _ GET [$ this-> pageName]);
}
} Else {
// $ Now handled manually
$ This-> _ nowPage = intval ($ now );//
}
// Set the url prefix
If (! Empty ($ url )){
// $ The url has been manually processed
$ This-> _ preUrl = $ url. (stristr ($ url ,'? ')? '&':'? '). $ This-> pageName.' = ';
} Else {
// Automatically obtain
If (empty ($ _ SERVER ['query _ string']) {
// No query exists in the url
$ This-> _ preUrl = $ _ SERVER ['request _ URI '].'? '. $ This-> pageName.' = ';
} Else {
If (stristr ($ _ SERVER ['query _ string'], $ this-> pageName. '= ')){
// Page = n (2, 3...) is found in the query ...)
$ This-> _ preUrl = str_replace ($ this-> pageName. '= '. $ this-> _ nowPage, '', $ _ SERVER ['request _ URI ']);
$ LastCharacter = $ this-> _ preUrl [strlen ($ this-> _ preUrl)-1];
If ($ lastCharacter = '? '| $ LastCharacter = '&'){
// Page = n (2, 3...) at the end of the original url
$ This-> _ preUrl. = $ this-> pageName. '= ';
} Else {
// Page = n (2, 3...) is not at the end of the original url
$ This-> _ preUrl. = '&'. $ this-> pageName. '= ';
}
} Else {
// No page = n (2, 3...) is found in the query ...)
$ This-> preUrl = $ _ SERVER ['request _ URI '].' & '. $ this-> pageName.' = ';
}
}
}
}
// Set the link address
Private function _ setUrl ($ pageNo ){
Return $ this-> _ preUrl. $ pageNo;
}
// Set the link
Private function _ setLink ($ url, $ link, $ style = ''){
$ Style = 'class = "'. $ style .'"';
Return ''. $ link .'';
}
}
//

Usage

$ Total = 1245;
// Class Page ($ nums, $ per = '', $ preNow ='', $ barNum = '', $ now ='', $ url = '')
$ Page = new Page ($ total );
$ Pager = $ page-> showPage ();
$ Smarty-> assign ('pager', $ pager );
$ Smarty-> display ('index. tpl ');

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.