您的位置:首页 > 编程语言 > PHP开发

PHP分页技术详解

2014-01-24 10:55 316 查看
直接上代码,注释很详细了。

<?php
/**
* php分页技术详解
* @author jahng
*/
header("Content-type: text/html; charset=UTF-8");
echo'<link rel="stylesheet" type="text/css" href="css.css" media="all" />';
require_once('page.php');
$db = @mysql_connect("localhost","root","haojiang");
if($db==false){
exit("连接数据库失败!");
}
if(!mysql_select_db("gbook", $db)){
exit("gbook数据库不存在!");
}
//从数据库获取将要分页的数据的总记录数
$result=mysql_query("SELECT COUNT(*) AS c FROM message");
$allRecord=mysql_fetch_assoc($result);
$count=$allRecord['c'];
//获取当前页
$currentPage=isset($_GET['page'])?intval($_GET['page']):1;
//每页显示五条记录
$limit=5;
//计算当前页的第一条数据在数据库中的id
$start=($currentPage-1)*$limit;
//从数据库获取当前页将要显示的数据
$sql="SELECT * FROM message LIMIT $start,$limit";
$result=mysql_query($sql);
$message=array();
if($result && mysql_num_rows($result)!=0){
while($r=mysql_fetch_assoc($result)){
$message[]=$r;
}
}
//简单的留言显示
foreach($message as $item){
echo "<dt>".date('Y-m-d h:i:s',$item['time'])." ".$item['name']."</dt>";
echo "<dd>".$item['content']."</dd>";
}
//分页链接
echo page($currentPage,$count,$limit,5,$class='sabrosus');
?>


效果图:



demo可以在 PHP分页demo 下载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: