PHP pagination Sample Code _php basics

Source: Internet
Author: User
<?php
/*
requirements, build a test database, build a test table inside it,
As long as the ID field, enter the data is OK ...
Because the level is limited, inevitable error.
*/
$conn = mysql_connect ("localhost", "root", "");
$maxnum = 2; Display record bars per page
mysql_select_db ("Test", $conn);
$query 1 = "Select COUNT (*) as totalrows from Test";
$result 1 = mysql_query ($query 1, $conn) or Die (Mysql_error ());
$row 1 = mysql_fetch_assoc ($result 1);
$totalRows 1 = $row 1[' totalrows ']; Data set data total number of bars
$totalpages = Ceil ($totalRows 1/$maxnum);//Calculate the total number of pagination, ceil () is the UP function
if (!isset ($_get[' page ')) | | |!intval ($_get[' page ') | | $_get[' page '] > $totalpages) $page = 1; Default handling of 3 types of errors
When a page is not 10 in number when the URL parameter page does not exist, the default is 1 when the page is greater than the page can be divided
else $page = $_get[' page '];
$startnum = ($page-1) * $MAXNUM; Starting from the dataset $startnum, note that the dataset starts at 0
$query = "SELECT * FROM Test LIMIT $startnum, $maxnum"//Select the data that meets the requirements from the $startnum data, choose $maxnum Line
$result = mysql_query ($query, $conn) or Die (Mysql_error ());
$row = Mysql_fetch_assoc ($result);
?>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Pagination Example </title>
<script language= "JavaScript" type= "Text/javascript" >
<!--
function Mm_jumpmenu (targ,selobj,restore) {//v3.0
Eval (targ+ "location=" "+selobj.options[selobj.selectedindex].value+");
if (restore) selobj.selectedindex=0;
}
-->
</script>
<style type= "Text/css" >
A{text-decoration:none;}
A:hover{text-decoration:underline}
table{font-size:12px;}
. Tb{background-color: #73BB95}
. Tr{background-color: #FFFFFF}
</style>

<body>
<table width= "30%" border= "0" align= "center" cellpadding= "0" cellspacing= "1" class= "TB" >
<tr>
&LT;TD height= ><div align= "left" > Pagination example </div></td>
</tr>
<?php if ($totalRows 1) {//Recordset not empty display
do {
?>
&LT;TR class= "TR" >
&LT;TD height= "><div align=" center "><?php echo $row [' id '];? ></div></td>
</tr>
<?php}while ($row = Mysql_fetch_assoc ($result));? >
</table>
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr><form name= "Form1" >
&LT;TD height= "><div" align= "Center" >
<?php
echo "Total <font color=" #ff0000 "> $totalRows 1</font> Record";
echo "<font color=" #ff0000 ">". $page. " </font> "." /". $totalpages." Page ";
Implementation << < 1 2 3 4 5> >> pagination link
$pre = $page -1;//Previous page
$next = $page + 1;//Next page
$maxpages = 4;//Processing page << < 1 2 3 4 > >> Show 4 page
$pagepre = 1;//If the current page is 4, also display the front $pagepre page, such as << < 3/4/5 6 > >> to show page 3rd
if ($page!= 1) {echo "<a href= '". $_server[' Php_self ']. "' ><<</a> ";
echo "<a href= '". $_server[' php_self '. Page= '. $pre. "' ><</a> ";}
if ($maxpages >= $totalpages)//If the total record is not sufficient to display 4 pages
{$pgstart = 1; $pgend = $totalpages;} Not so the page print processing
ElseIf ($page-$pagepre -1+ $maxpages) > $totalpages)//As if the total number of pages is 6, currently 5, you have to show 3 4 before, not just 4
{$pgstart = $totalpages-$maxpages + 1; $pgend = $totalpages;}
else{
$pgstart = ($page <= $pagepre)? 1: ($page-$pagepre))//The current page is 1 o'clock, will be 1 2 3 4 > >> not 0 1 2 3 > >>
$pgend = (($pgstart ==1) $maxpages:($pgstart + $maxpages-1));
}
for ($pg = $pgstart; $pg <= $pgend; $PG + +) {//Jump Menu
if ($pg = = $page) echo "<a href=" ". $_server[' php_self '." Page= $pg "><font color=" #ff0000 "> $pg </font></a>";
else echo "<a href=" ". $_server[' php_self '." Page= $pg "> $pg </a>";
}
if ($page!= $totalpages)
{echo "<a href= '". $_server[' Php_self ']. Page= '. $next. "' >></a> ";
echo "<a href= '". $_server[' php_self '. Page= '. $totalpages. "' >>></a> ";}
?>
<select name= "menu1" onchange= "Mm_jumpmenu" (' Parent ', this,0) ">
<option value= "" > Selection </option>
<?php for ($pg 1=1; $pg 1<= $totalpages; $pg 1++) {
echo "<option value=" ". $_server[' php_self '." page= $pg 1 ">". $PG 1. " </option> ";
}?>
</select>
</td></form>
</tr>
</table>
<?php} else {//Recordset is empty to display?>
&LT;TR class= "TR" >
&LT;TD height= "><div" align= "center" > No record </div></td>
</tr>
</table>
<?php}?>
</body>

<?php
Mysql_free_result ($result 1);
Mysql_free_result ($result);
?>

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.