首頁  >  文章  >  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