首页 > php教程 > php手册 > php 分页实现代码

php 分页实现代码

WBOY
发布: 2016-06-06 19:38:52
原创
1375 人浏览过

无详细内容 无 ?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>
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板