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;
}