PHP pagination Instance with code

Source: Internet
Author: User

A typical PHP page instance code sharing, learning PHP friends must be used to get, mainly to understand the idea:

<! 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 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_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 page </a>";//Next page

$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>

Go see what you know? " color code Daquan

PHP pagination Instance with code

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.