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();
}