Paging functionality is often used in web sites, so the paging functionality is encapsulated into a function that reduces programming effort when invoked. 1. Paging function Design: Here the paging function input has five parameters: the total number of records $recordtotalnum, the number of records displayed per page $perpagesize, the page currently displayed $page, the current page $url, query keyword $ keyword, you can call the paging function as long as you set these 5 parameters (not designed without the query keyword $keyword). The function of the paging function is primarily the link to output pagination, and the following is the page () function code in the paging function 1.php:
<!--1.php paging function--> <?php functions page ($RecordTotalNum, $PerPageSize, $Page, $url, $keyword) {$PageCount =ceil ($ recordtotalnum/$PerPageSize); Calculate Total pages $page _previous= ($Page <=1) 1: ($Page-1); Calculate the number of pages on the previous page $page _next= ($Page >= $PageCount)? $PageCount:($Page + 1); Calculate the number of pages on the next page $page _start= ($Page -5>0)? ($Page-5): 0; Show page links only on the first 5 pages of this page $page _end= ($page _start+10< $PageCount)? ($page _start+10): $PageCount;
Show only five pages after the link//If more than 10 pages, only show the page before and after the link $page _start= $page _end-10; if ($page _start<0) $page _start=0; If the current is not valid, correct $parse _url=parse_url ($url); To determine if there is a "URL string" if (Empty ($parse _url[' query ')) in $url $url = $url. '? ';
If it does not exist, add it after $url? else $url = $url. ' & '; If present, add &//var_dump after $url ($RecordTotalNum, $PerPageSize, $Page, $url, $keyword, $page _start, $page _end);
For debugging if (empty ($keyword)) {if ($Page ==1) $navigator = "[First] [prev]"; else $navigator = "<a href="? Page=1 ' >[home]</a> <a href= ". $url." page= $page _previous>[prev]</a> ";
for ($i = $page _start $i < $page _end $i + +) {$j = $i +1; if ($j = = $Page) $navigator = $navigator. "
$j "; else $navigator = $navigator. " <a href= ' ". $url."
Page= $j ' > $j </a> '; if ($Page = = $PageCount) $navigator = $navigator. " [Next Page]
[Last] "; else $navigator = $navigator. "<a href=". $url. " page= $page _next>[Next page]</a> <a href= ". $url."
Page= $PageCount >[last]</a> "; $navigator. = " altogether". $RecordTotalNum. "
Records $Page/$PageCount page ";
else {//If the query keyword is set, add the query keyword to the URL link if ($Page ==1) $navigator = "[First] [prev]"; else $navigator = "<a href="? Page=1 ' >[home]</a> <a href= ". $url."
keyword= $keyword &page= $page _previous>[prev]</a> ";
for ($i = $page _start $i < $page _end $i + +) {$j = $i +1; if ($j = = $Page) $navigator = $navigator. "
$j "; else $navigator = $navigator. " <a href= ' ". $url."
keyword= $keyword &page= $j ' > $j </a> '; } if ($Page = = $PageCount)
$navigator = $navigator. " [Next Page]
[Last] "; else $navigator = $navigator. "<a href=". $url. " keyword= $keyword &page= $page _next>[next page]</a> <a href= ". $url."
keyword= $keyword &page= $PageCount >[last]</a> "; $navigator. = " altogether". $RecordTotalNum. "
Records $Page/$PageCount page "; Echo $navigator;
Output Paging Link}?>
2. Examples of paging functions:
The following code implements pagination by calling the paging function, as follows:
<!--conn.php connection MySQL database-->
<?php
$conn =mysql_connect ("localhost", "root", "098933mabing"); Connect the database server
mysql_query ("Set names ' Utf-8 '"); Sets the character set
mysql_select_db ("Guestbook", $conn); Select Database
?>
<!--2.php calls the paging function to implement the example of pagination--> <?php require ("1.php");
Call the paging function file require ("conn.php");
if (Isset ($_get[' page ') && (int) $_get[' page ']>0)//Get page number and check if illegal $Page =$_get[' page ']; else $Page = 1; If the page number is not obtained, the first page is displayed $keyword =$_get[' keyword ']; Obtain the keyword if ($keyword <> "") $result =mysql_query ("select * Lyb where title like '% $keyword% '", $conn);
Fuzzy matching else $result =mysql_query ("SELECT * from Lyb", $conn); $RecordTotalNum =mysql_num_rows ($result); Total number of records if ($RecordTotalNum <=0) {//To determine if the query result is null echo <script> alert (' No title has been searched for the keyword. '); location.href= ' 7.3.3-2.php '; </script> "; Refresh exit ();
Early exit Script} if (Isset ($_get[' perpagesize ')) {//The number of records displayed per page $PerPageSize =$_get[' perpagesize ']; $_session["Perpagesize"]=$_get[' perpagesize ']; This value is saved to the session variable to prevent the page $perpagesize from switching back to the default value} if ($_session["Perpagesize"]>0)//If the session variable is not empty (more than $recordtotalnum
will display the maximum number of records) $PerPageSize =$_session["Perpagesize"];
Else$PerPageSize = 2; The default display of 2?> <form method= "get" action= "> <div style=" border:1px Solid: #eee When the first entry or entry is illegal padding:4px; " > According to the title to find a message: Please enter the keyword <input type= "text" name= "keyword" value= "<?= $keyword?>"/> <!--when the keyword is entered, the input box has been Show keyword--> <input type= "Submit" value= "Query"/> </div> </form>
"Note" This program contains a table named Lyb in advance in the MySQL server's guestbook database.