PHP Pagination Instance Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>php Tutorial Make page list</title>
<style type= "Text/css" >
<!--
. page A:link {
Color: #0000FF;
Text-decoration:none;
}
. Page a:visited {
Text-decoration:none;
Color: #0000FF;
}
. page A:hover {
Text-decoration:none;
Color: #0000FF;
}
. Page A:active {
Text-decoration:none;
Color: #0000FF;
}
. Page{color: #0000FF;}
-
</style>
<body>
<table width= "530" height= "103" border= "0" align= "center" cellpadding= "0" cellspacing= "1" bgcolor= "#CCCCCC" >
<tr>
<th width= "height=" bgcolor= "#E3E3E3" scope= "col" >ID</th>
<th width= "bgcolor=" #E3E3E3 "scope=" col "> article title </th>
</tr>
<?php
/*
* Created on 2010-4-17
*
* Order by Kove Wong
*/
$link =mysql_connect (' localhost ', ' root ', ' haoxiazai1987 ');
MySQL tutorial _select_db (' pagelist ');
mysql_query (' Set names GBK ');
$Page _size=10;
$result =mysql_query (' select * from V_char ');
$count = mysql_num_rows ($result);
$page _count = ceil ($count/$Page _size);
$init = 1;
$page _len=7;
$max _p= $page _count;
$pages = $page _count;
Judging the current page number
if (Empty ($_get[' page ') | | $_get[' page ']<0) {
$page = 1;
}else {
$page =$_get[' page ';
}
$offset = $Page _size* ($page-1);
$sql = "SELECT * from V_char limit $offset, $Page _size";
$result =mysql_query ($sql, $link);
while ($row =mysql_fetch_array ($result)) {
?>
<tr>
<TD bgcolor= "#E0EEE0" height= "25px" ><div align= "center" >
<?php echo $row [' ID ']?>
</div></td>
<TD bgcolor= "#E0EEE" ><div align= "center" >
<?php echo $row [' Name ']?>
</div></td>
</tr>
<?php
}
$page _len = ($page _len%2)? $page _len: $pagelen +1;//page number
$pageoffset = ($page _len-1)/2;//page number offset
$key = ' <div class= ' page > ';
$key. = "<span> $page/$pages </span> "; Page, total pages
if ($page!=1) {
$key. = "<a href=" ". $_server[' Php_self ']."? Page=1 "> First page </a>"; First page
$key. = "<a href=" ". $_server[' Php_self ']."? Page= ". ($page-1). "" > Prev </a> "; Previous page
}else {
$key. = "First page";//First page
$key. = "Previous page"; Previous page
}
if ($pages > $page _len) {
If the current page is less than or equal to the left offset
if ($page <= $pageoffset) {
$init = 1;
$max _p = $page _len;
}else{//If the current page is greater than the left offset
If the right offset of the current page number exceeds the Max page count
if ($page + $pageoffset >= $pages + 1) {
$init = $pages-$page _len+1;
}else{
Calculations when left and right offsets are present
$init = $page-$pageoffset;
$max _p = $page + $pageoffset;
}
}
}
for ($i = $init; $i <= $max _p; $i + +) {
if ($i = = $page) {
$key. = ' <span> ' $i. ' </span> ';
} else {
$key. = "<a href=" ". $_server[' Php_self ']."? Page= ". $i." " > ". $i." </a> ";
}
}
if ($page! = $pages) {
$key. = "<a href=" ". $_server[' Php_self ']."? Page= ". ($page + 1). "" > Next </a> ";//[Next]
$key. = "<a href=" ". $_server[' Php_self ']."? page={$pages} "> Last page </a>"; Last page
}else {
$key. = "Next page";//Next page
$key. = "Last page"; Last page
}
$key. = ' </div> ';
?>
<tr>
<TD colspan= "2" bgcolor= "#E0EEE0" ><div align= "center" ><?php Echo $key?></div></td>
</tr>
</table>
</body>
pagination Example (PHP)