A useful paging function

Source: Internet
Author: User
A useful paging function The code is as follows:


In my original code, it seems that the process may be clumsy, but it is very practical.

/*---------------------------------------------------------------//
* Function description: paging function page ($ SQL, $ pagesize = "30 ")
* $ SQL query statement (except the limit statement, which can be sorted or restricted by conditions)
* For example, select * from stu where time between "1" and "30 ";
* $ Pagesize the number of entries displayed per page
* ## The $ arr value of the output array is described as follows:
* $ Arr ["first"] homepage and address
* $ Arr ["page_pre"] previous page and address
* $ Arr ["all"] when the page number and total page number
* $ Arr ["page_next"] next page and address
* $ Arr ["last"] last page and address
* $ Arr ["pagelist"] page number list and address. the list of four pages before and after the current page is displayed.
* $ Arr ["query"] statement $ arr ["query"] = mysql_query ($ SQL)
* $ Arr ["nums"] total number of records
* 2006.09.06 by Kevin QQ: 84529890
//----------------------------------------------------------------*/
Function page ($ SQL, $ pagesize = "30 "){

Global $ arr, $ PHP_SELF;

$ Query = mysql_query ($ SQL );
$ Num = mysql_num_rows ($ query );
$ Pagecount = ceil ($ num/$ pagesize );
$ Page = $ _ GET ["page"];
If (! $ Page) $ page = 1;
If ($ page> $ pagecount) $ page = $ pagecount;

$ Offset = ($ page-1) * $ pagesize;
$ SQL. = "limit $ offset, $ pagesize ";
$ Arr ["query"] = mysql_query ($ SQL );

If ($ page> 1 ){
$ Page_pre = $ page-1;
$ Page_url = $ PHP_SELF ."? Page = ". $ page_pre;
$ Arr ["page_pre"] = "previous page | \ n ";
}
If ($ page <$ pagecount ){
$ Page_next = $ page + 1;
$ Page_url = $ PHP_SELF ."? Page = ". $ page_next;
$ Arr ["page_next"] = "| Next Page \ n ";
}

$ Arr ["all"] = "". $ page. "/". $ pagecount. "page ";
$ Arr ["first"] = "homepage \ n | ";
$ Arr ["last"] = "| last Page \ n ";

$ Plfront = "";
If ($ page <= 5 & $ page> = 1 ){
For ($ I = 1; $ I <= 9; $ I ++ ){
$ Plfront. = "". $ I ."";
}
} Elseif ($ page> 5 & $ page <$ pagecount-5 ){
For ($ I = $ page-4; $ I <$ page + 5; $ I ++ ){
$ Plfront. = "". $ I ."";
}
} Else {
For ($ I = $ pagecount-8; $ I <= $ pagecount; $ I ++ ){
$ Plfront. = "". $ I ."";
}
}

$ Arr ["pagelist"] = $ plfront ."";
$ Arr ["nums"] = $ num;
}

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.