PHP分页过程化及分页类实现

 

过程化分页

 

<?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.'页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从第'.$this->start.'条记录到第'.$this->end.'条记录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共'.$this->count.'页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$this->url.'page=1">'.$this->first.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

		if($this->prevNum){
			$string.='<a href="'.$this->url.'page='.$this->prevNum.'">'.$this->prev.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

		}else{
			$string.=$this->prev.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}


		if($this->nextNum){
			$string.='<a href="'.$this->url.'page='.$this->nextNum.'">'.$this->next.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

		}else{
			$string.=$this->next.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}

		$string.='<a href="'.$this->url.'page='.$this->count.'">'.$this->last.'</a>';

	
		return $string;
	}

}




?>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值