PHP simple paging code implementation

Source: Internet
Author: User
PHP simple paging code implementation ** @ todo paging * @ param $ count total number of lines * @ param $ pagesize number of lines per page * @ param $ nowpagenum current page number * @ returnStringHTML * @ example * $ count45; total data queried by the database $ page PHP simple paging code implementation

/**
* @ Todo paging
* @ Param $ count the total number of items
* @ Param $ pagesize the number of entries displayed on each page
* @ Param $ nowpagenum current page number
* @ Return String HTML
* @ Example
* $ Count = 45; // total data queried by the database
$ Pagesize = 10; // Number of Entries displayed on each page
$ Nowpagenum = isset ($ _ REQUEST ['Page'])? $ _ REQUEST ['Page']: 1; // Obtain the current page number. the default value is 1 when no page is found.
$ Page = $ this-> PageNum ($ count, $ pagesize, $ nowpagenum );
*
*/
Public function PageNum ($ count, $ pagesize, $ nowpagenum ){
// Calculate the page number
$ Pagenum = ceil ($ count/$ pagesize ));
/*** Set the default value of 1 when no GET page is displayed on the first page ***/
$ Nowpagenum = empty ($ nowpagenum )? 1: $ nowpagenum;
/***** Resolve the URL ******/
$ Url = $ _ SERVER ['request _ URI '];
$ UrlArr = explode ('? ', $ Url );
If (count ($ urlArr) = 1 ){
$ Newurl = $ url .'? Page = ';
} Else {
$ Params = array ();
$ Newurl = '';
$ NewParams = array ();
$ ParamsArr = explode ('&', $ urlArr [1]);
If (count ($ paramsArr) = 1 ){
$ GetParams = explode ('=', $ paramsArr [0]);
$ Params [$ getParams [0] = $ getParams [1];
$ Newurl = $ urlArr [0]. '? Page = ';
} Else {
For ($ j = 0; $ j
$ GetParams = explode ('=', $ paramsArr [$ j]);
$ Params [$ getParams [0] = $ getParams [1];
}
If (isset ($ params ['Page']) {
Unset ($ params ['Page']);
Foreach ($ params as $ key => $ value ){
$ NewParams [] = $ key. '='. $ value;
}
$ Newurl = $ urlArr [0]. '? '. Implode (' & ', $ newParams).' & page = ';
} Else {
Foreach ($ params as $ key => $ value ){
$ NewParams [] = $ key. '='. $ value;
}
$ Newurl = $ urlArr [0]. '? '. Implode (' & ', $ newParams).' & page = ';
}
}
}
/***** Display HTML code on assembled page number ****/
$ Html = '';
If (isset ($ count) & isset ($ pagesize )){
// One page
If ($ count <= $ pagesize ){
$ Html = 'previous page 1 ';
// No record
} Else if ($ count = 0 ){
$ Html = 'not recorded yet! ';
// More than one page, less than or equal to six pages
} Else if ($ count> $ pagesize & $ pagenum <= 6 ){
If ($ nowpagenum = 1 ){
$ Html. = 'preview ';
} Else {
$ Html. = 'preview ';
}
For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
If ($ I ==$ nowpagenum ){
$ Html. = ''. $ I .'';
} Else {
$ Html. = ''. $ I .'';
}
}
If ($ nowpagenum ==$ pagenum ){
$ Html. = 'next page ';
} Else {
$ Html. = 'next page ';
}
} Else {
If ($ nowpagenum = 1 ){
$ Html. = 'preview ';
} Else {
$ Html. = 'preview ';
}
If ($ pagenum-$ nowpagenum)> 5 ){
If ($ nowpagenum <= 3 ){
For ($ I = 1; $ I <= 3; $ I ++ ){
If ($ I ==$ nowpagenum ){
$ Html. = ''. $ I .'';
} Else {
$ Html. = ''. $ I .'';
}
}
} Else {
For ($ I = $ nowpagenum-2; $ I <= $ nowpagenum; $ I ++ ){
If ($ I ==$ nowpagenum ){
$ Html. = ''. $ I .'';
} Else {
$ Html. = ''. $ I .'';
}
}
}
$ Html. = '...';
For ($ j = $ pagenum-2; $ j <= $ pagenum; $ j ++ ){
If ($ j = $ nowpagenum ){
$ Html. = ''. $ j .'';
} Else {
$ Html. = ''. $ j .'';
}
}
} Else {
For ($ I = $ pagenum-5; $ I <= $ pagenum; $ I ++ ){
If ($ I ==$ nowpagenum ){
$ Html. = ''. $ I .'';
} Else {
$ Html. = ''. $ I .'';
}
}
}
If ($ nowpagenum ==$ pagenum ){
$ Html. = 'next page ';
} Else {
$ Html. = 'next page ';
}
}
}
Return $ html;
}

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.