PHP paging: Text paging and numeric Paging,
Source: http://www.ido321.com/1086.html
Recently, pagination is required in the project. The paging function is often used. Therefore, it is encapsulated as a function.
// Paging/***** $ pageType paging type 1: Digital paging 2: Text paging * You can pass $ pageTotal, $ page, $ total, and other data as parameters, or use paging as a global variable (recommended) */function paging ($ pageType) {global $ pageTotal, $ page, $ total; if ($ pageType = 1) {echo '<div id = "pagenum">'; echo '<ul>'; for ($ I = 0; $ I <$ pageTotal; $ I ++) {if ($ page = ($ I + 1) {echo '<li> <a href = "blogfriends. php? Page = '. ($ I + 1 ). '"class =" selected "> '. ($ I + 1 ). '</a> </li>';} else {echo '<li> <a href = "blogfriends. php? Page = '. ($ I + 1 ). '"> '. ($ I + 1 ). '</a> </li>';} echo '</ul>'; echo '</div>';} else if ($ pageType = 2) {echo '<div id = "pagetext">'; echo '<ul>'; echo '<li> '. $ page. '/'. $ pageTotal. 'page | </li>'; echo '<li> total <strong> '. $ total. '</strong> Members | </li>'; // if ($ page = 1) {echo '<li> homepage | </li>' on the first page '; echo '<li> Previous Page | </li>';} else {// $ _ SERVER ["SCRIPT_NAME"] To get the current Script Name, convenient porting // You can also customize constants. The constant value is consistent with the script file name echo '<li> <a href = "'. $ _ SERVER ["SCRIPT_NAME"]. '"> homepage </a> | </li>'; echo '<li> <a href = "'. $ _ SERVER ["SCRIPT_NAME"]. '? Page = '. ($ page-1 ). '"> previous page </a >|</li>';} // if ($ page = $ pageTotal) on the last page) {echo '<li> next page | </li>'; echo '<li> last page | </li> ';} else {echo '<li> <a href = "'. $ _ SERVER ["SCRIPT_NAME"]. '? Page = '. ($ page + 1 ). '"> next page </a> | </li>'; echo '<li> <a href = "'. $ _ SERVER ["SCRIPT_NAME"]. '? Page = '. ($ pageTotal ). '"> last page </a> | </li>';} echo '</ul>'; echo '</div> ';}}
Parameter description:
$ PageTotal indicates the total number of pages, $ page indicates the current page, and $ total indicates the total number of data retrieved from the database;
To simplify the process, encapsulate all parameters.
// Paging parameter package/*** $ SQL can be used to obtain the total number of data in an SQL statement * $ size the number of entries displayed on each page */function pageParam ($ SQL, $ size) {// set global variables for all involved parameters // $ where to start a page of pagestart // $ total number of records $ page a page $ total pageTotal pages global $ pagestart, $ pagesize, $ total, $ page, $ pageTotal; $ pagesize = $ size; // total number of Retrieved Data $ total = mysql_num_rows (queryDB ($ SQL); // error handling, first, determine whether there is an if (isset ($ _ GET ['page']) {// a specific page $ page = $ _ GET ['page']; // determine whether it is empty (0 is empty)/less than 0/whether it is a number if (empty ($ pag E) | $ page <0 |! Is_numeric ($ page) {$ page = 1;} else {$ page = intval ($ page); // integer, prevent decimal occurrence} else {// initialize display 1st page $ page = 1;} // CLEAR database if ($ total = 0) {// set to 1 $ pageTotal = 1;} total number of else {// pages (one-to-one integer) $ pageTotal = ceil ($ total/$ pagesize );} // The page number is greater than the total page number $ total if ($ page> $ pageTotal) {$ page = $ pageTotal ;} // when the page starts from a record $ pagestart = ($ page-1) * $ pagesize ;}
Parameter description:
$ Pagestart: When a page starts from a record, $ pagesize indicates the number of records displayed on each page.
In use, call pageParam first, and then call paging
/*** The first SQL statement that can obtain the total number of data * The number of entries displayed on the second page */pageParam ("select userid from user", 2 );
<? Php // paging type 1: Digital paging 2: Text paging (2);?>
Select the call location based on the actual situation. The text page is as follows:
<? Php // paging type 1: Digital paging 2: Text paging (1);?>
The number page is as follows:
Adjust the style.
Next article: small examples of Map Display by Google Maps API