php 分页类

class MulPage
{	
	
	//共多少个
	private $total_num;
	//共多少页 
	private $total_page;
	//每页几个
	private $pre_page_num;
	//当前页
	private $current_page;
	//当前页最低
	private $current_min;
	//当前页最高
	private $current_max;

	
	public function __construct( $total_num , $pre_page_num = 10 )
	{
		$this->total_num = $total_num ;
		$this->pre_page_num = $pre_page_num ;
		$this->total_page = ceil($this->total_num / $this->pre_page_num) ;
	}
	
	public function get_total_pages()
	{
		return $this->total_page;
	}

	public function  get_min()
	{
		$this->check_current_page();
		return ($this->current_page - 1) * $this->pre_page_num ;
	}
	
	public function  get_max()
	{
		$this->check_current_page();
		if ( ($this->current_page * $this->pre_page_num) > $this->total_num )
			return $this->total_num;
		return $this->current_page * $this->pre_page_num ;
	}
	
	public function set_current_page($curp)
	{
		if (is_numeric($curp))
		{
			if ($curp < 1 )
				$this->current_page = 1;
			else if ($curp > $this->total_page )
				$this->current_page = $this->total_page;
			else 
				$this->current_page = $curp;
		}
		return $this;
	}
	
	public function throw_exception( $error )
	{
		throw new Exception( $error );
	}
	
	public function check_current_page()
	{
		if ( empty($this->current_page) || !$this->current_page || (0 == $this->current_page ) )
			$this->throw_exception("<h1>must set current page</h1>");
	}
}


运行如这样:


$mp = new MulPage(127,15);

try 
{	
	echo "total page : " . $mp->set_current_page(7)->get_total_pages();
	echo "<br/>" ;
	echo "max : "  . $mp->get_max();
	echo "<br/>";
	echo "min : "  . $mp->get_min();
}
catch (Exception $e)
{
	echo $e->__toString();	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值