A typical PHP paging instance code

Source: Internet
Author: User
Tags count prev

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head>


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


</head>


<body>


<table width= "530" height= "0" border= "center" align= "0" cellpadding= "1" cellspacing= "#CCCCCC" >


<tr>


<th width= "height=" bgcolor= "#E3E3E3" scope= "col" >ID</th>


<th width= "a" 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;





//Judge 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;//the left or right offset of page number





$key = ' <div class= ' page ' > ';


$key. = "<span> $page/$pages </span> "; First page, a total of several 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. = "Prev"; 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 current page number right offset exceeds the maximum paging page


if ($page + $pageoffset >= $pages + 1) {


$init = $pages-$page _len+1;


}else{

Calculation of
//left and right offsets when they exist


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


$key. = "<a href=" ". $_server[' Php_self ']." page={$pages} "> Last page </a>"; Last page


}else {


$key. = "Next page";//Next


$key. = "Last page"; Last page


 }


$key. = ' </div> ';


?>


<tr>


<td colspan= "2" bgcolor= "#E0EEE0" ><div align= "center" ><?php Echo $key? ></div></td&gt ;


</tr>


</table>


</body>


</html>

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.