>  기사  >  php教程  >  php 分页实现代码

php 分页实现代码

WBOY
WBOY원래의
2016-06-06 19:38:521332검색

无详细内容 无 ?php //数据库连接$my_db = mysql_connect("localhost","root","");mysql_select_db("model", $my_db);mysql_query("set names 'utf8'");$page = isset($_GET['page'])?intval($_GET['page']):0;//第几页值$shownum = 2;//每页显示数量$limit =

<?php 
//数据库连接
$my_db = mysql_connect("localhost","root","");
mysql_select_db("model", $my_db);
mysql_query("set names &#39;utf8&#39;");
$page = isset($_GET[&#39;page&#39;])?intval($_GET[&#39;page&#39;]):0;//第几页值
$shownum = 2;//每页显示数量
$limit = $page * $shownum;//查询开始数
$sql = "select * from web_news order by id desc limit $limit,$shownum";
$query = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_assoc($query))
{
  $rows[] = $row;
}
$sql_count = "select * from web_news";
$total = mysql_num_rows(mysql_query($sql_count));//总数量
$pagenum = ceil($total/$shownum);//总共页数
$pages = &#39;&#39;;
$pages .=&#39;总共&#39;.$total.&#39;信息&#39;.&#39; 共&#39;.$pagenum.&#39;页,每页&#39;.$shownum.&#39;条信息&#39;;
$pages .=&#39;<a href="page.php?page=0">&#39;.&#39;首页&#39;.&#39;</a>&#39;;
if($page <= 0){
   $pages .=&#39;<a href="page.php?page=&#39;.$page.&#39;">&#39;.&#39;上一页&#39;.&#39;</a>&#39;;
}else{
   $pages .=&#39;<a href="page.php?page=&#39;.($page-1).&#39;">&#39;.&#39;上一页&#39;.&#39;</a>&#39;;
}
for($i=0;$i<$pagenum;$i++){
	if($page == $i){
	   $pages .=&#39;<a class="hover">&#39;.($i+1).&#39;</a>&#39;;
	}else{
	   $pages .=&#39;<a href="page.php?page=&#39;.$i.&#39;">&#39;.($i+1).&#39;</a>&#39;;
	}
}

if($page >= ($pagenum-1)){
   $pages .=&#39;<a href="page.php?page=&#39;.($pagenum-1).&#39;">&#39;.&#39;下一页&#39;.&#39;</a>&#39;;
}else{
   $pages .=&#39;<a href="page.php?page=&#39;.($page+1).&#39;">&#39;.&#39;下一页&#39;.&#39;</a>&#39;;
}
$pages .=&#39;<a href="page.php?page=&#39;.($pagenum-1).&#39;">&#39;.&#39;末页&#39;.&#39;</a>&#39;;
?>

<!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=utf-8">
<title>分页</title>
</head>
<style>
.pages a{ padding:5px 8px 5px 8px; border:#999999 1px solid; margin-left:5px;}
.pages a.hover{ color:#FF0000;}
</style>
<body>
<div>
<ul>
<?php foreach($rows as $val){?>
<li><?php echo $val[&#39;title&#39;];?></li>
<?php }?>
</ul>
<div class="pages"><?php echo $pages;?></div>
</div>
</body>
</html>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.