过程化分页
<?php
header("Content-Type:text/html;charset=utf-8");
mysql_connect('localhost','root','root');
mysql_select_db('test');
$sql="select * from test where 1";
$result=mysql_query($sql);
$total_num=mysql_num_rows($result);//查出一共有多少条记录
$show_num=3;//显示多少条
$total_pages=ceil($total_num/$show_num);//获取总的页数,ceil向上去整,floor向下
$current=isset($_GET['page'])?$_GET['page']:1;//当前页号
$next=($current==$total_pages)?false:$current+1;
$prev=($current==1)?false:$current-1;
$offset=($current-1)*$show_num;
$sql="select * from goods limit $offset,3";//offset为偏移量,代表查询时候,数据库起始位置
$result=mysql_query($sql);
mysql_close();
?>
<table>
<tr><th>id</th><th>name</th><th>price</th><th>mprice</th></tr>
<?php while($arr=mysql_fetch_assoc($result)){
$id=$arr['id'];
$name=$arr['name'];
$price=$arr['price'];
$mprice=$arr['mprice'];
?>
<tr><td><?php echo $id ?></td><td><?php echo $name ?></td><td><?php echo $price ?></td><td><?php echo $mprice ?></td></tr>
<?php } ?>
<tr><td colspan="4">
<?php
echo "一共有{$total_num}条记录,显示{$show_num}条,{$current}/{$total_pages}";
echo "首页";
if(!$prev){
echo "上一页";
}else{
echo "<a href='fenye.php?page={$prev}'>上一页</a>";
}
if(!$next){
echo "下一页";
}else{
echo "<a href='fenye.php?page={$next}'>下一页</a>";
}
echo "尾页";
unset($result);
?>
</td></tr>
</table>
下面是分页类:
<?php
class page{
//总页数
protected $count;
//当前页码
protected $page;
//总记录数,总条数
protected $total;
//上一页
protected $prev='上一页';
//下一页
protected $next='下一页';
//首页
protected $first='首页';
//尾页
protected $last='尾页';
//开始记录数
protected $start;
//结束记录数
protected $end;
//每页显示条数
protected $num;
//URL
protected $url;
//上一页数
protected $prevNum;
//下一页数
protected $nextNum;
//初使化成员属性
public function __construct($url,$total,$num=5){
$this->url=$url;
$this->total=$total;
$this->num=$num;
$this->count=$this->getCount();
$this->page=empty($_GET['page'])?1:(int)$_GET['page'];
$this->prevNum=$this->getPrev();
$this->nextNum=$this->getNext();
$this->start=$this->getStart();
$this->end=$this->getEnd();
}
protected function getStart(){
return ($this->page-1)*$this->num+1;
}
protected function getEnd(){
return min($this->page*$this->num,$this->total);
}
protected function getNext(){
if($this->page>=$this->count){
return false;
}else{
return $this->page+1;
}
}
protected function getPrev(){
if($this->page<=1){
return false;
}else{
return $this->page-1;
}
}
protected function getCount(){
return ceil($this->total/$this->num);
}
//
//
//得到偏移量 limit 偏移量,数量 limit 0,5 5,5 10,5
public function getOffset(){
return ($this->page-1)*$this->num;
}
//得到分页效果
//
//search.php?keywords=手机&page=1
//
//第x页 从第n条记录到第n条记录 共x页 首页 上一页 下一页 尾页
public function getPage(){
$string='第'.$this->page.'页 从第'.$this->start.'条记录到第'.$this->end.'条记录 共'.$this->count.'页 <a href="'.$this->url.'page=1">'.$this->first.'</a> ';
if($this->prevNum){
$string.='<a href="'.$this->url.'page='.$this->prevNum.'">'.$this->prev.'</a> ';
}else{
$string.=$this->prev.' ';
}
if($this->nextNum){
$string.='<a href="'.$this->url.'page='.$this->nextNum.'">'.$this->next.'</a> ';
}else{
$string.=$this->next.' ';
}
$string.='<a href="'.$this->url.'page='.$this->count.'">'.$this->last.'</a>';
return $string;
}
}
?>