给大家分享一个PHP的分页类,是在我同事远哥的基础上加以改进而来的。
class Page
{
private static $item_count; //总数量
private static $current_page; //当前页码
private static $page_count; //总页数
private static $page_size; //每页包含多少条数据
private static $offset = 0; //偏移量
private static $query; //查询参数
/**
* @param $total 数据总数
* @param $page_size 每页展示的数据量
* @param array $additional 额外的请求参数
* @return mixed
*/
public static function init($total, $page_size, $additional = array())
{
$current_page = !empty($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
self::$current_page = intval($current_page);
self::$item_count = intval($total);
self::$page_size = intval($page_size);
self::$page_count = ceil($total / $page_size);
self::$query = array_merge($_REQUEST, $additional);
unset(self::$query["page"]);
self::computeOffset();
$page_data["offset"] = self::$offset;
$page_data["page_list"] = self::buildPageHtml();
return $page_data;
}
/**
* 计算偏移量
*/
private static function computeOffset()
{
if(self::$page_count < self::$current_page ){
self::$current_page = self::$page_count;
}
if(self::$current_page < 1){
self::$current_page = 1;
}
self::$offset = (self::$current_page - 1) * self::$page_size;
}
/**
* 生成分页HTML代码
* @return string
*/
private static function buildPageHtml()
{
$html = "";
$html .= self::buildPageMsg();
$html .= self::buildPrevHtml();
$html .= self::buildListHtml();
$html .= self::buildNextHtml();
$html .= self::buildSelectHtml();
return $html;
}
/**
* 页码列表
* @return string
*/
private static function buildListHtml()
{
$num = 3;
$num_result = 7;
$_html = array();
$current_page = self::$current_page;
if($current_page > self::$page_count){
$current_page = self::$page_count;
}
if($current_page < 1){
$current_page = 1;
}
$start_page = $current_page - $num < 1 ? 1 : $current_page - $num ;
$end_page = $current_page + $num > self::$page_count ? self::$page_count : $current_page + $num;
if($end_page - $start_page < $num_result - 1){
if($start_page <= $num ){
$end_page += ($num - $current_page + 1 );
}
if(($_num = $end_page - $current_page) <= $num){
$start_page -= $num - $_num ;
}
}
if($end_page > self::$page_count){
$end_page = self::$page_count;
}
if($start_page < 1){
$start_page = 1;
}
for($i=$start_page ;$i<=$end_page;$i++){
self::$query["page"] = $i;
$query = http_build_query(self::$query);
$item_html = "<a href='?".$query."' class='pageItem'>{$i}</a>";
if($i == $current_page){
$item_html = "<span class='pageItem currentItem'>{$current_page}</span>";
}
array_push($_html,$item_html);
}
return implode(" ",$_html);
}
/**
* 生成分页前缀信息
* @return string
*/
private static function buildPageMsg()
{
$current_page = self::$current_page;
$countPage = self::$page_count;
return "<span class='prefix'>{$current_page}/{$countPage}</span>";
}
private static function buildPrevHtml()
{
$html = "";
if(self::$page_count <= 1 || self::$current_page == 1){
$html .= "<span class='firstPage'>首页</span>";
$html .= "<span class='prev'>上一页</span>";
}else{
self::$query["page"] = 1;
$html .= "<a href='?".http_build_query(self::$query)."' class='firstPage'>首页</a>";
self::$query["page"] = self::$current_page - 1;
$html .= "<a href='?".http_build_query(self::$query)."' class='prev'>上一页</a>";
}
return $html;
}
private static function buildNextHtml()
{
$html = "";
if(self::$page_count <= 1 || self::$current_page == self::$page_count){
$html .= "<span class='next'>下一页</span>";
$html .= "<span class='lastPage'>尾页</span>";
}else{
self::$query["page"] = self::$current_page + 1;
$html .= "<a href='?".http_build_query(self::$query)."' class='next'>下一页</a>";
self::$query["page"] = self::$page_count;
$html .= "<a href='?".http_build_query(self::$query)."' class='lastPage'>尾页</a>";
}
return $html;
}
private static function buildSelectHtml()
{
unset(self::$query['page']);
$http_build_query = http_build_query(self::$query);
$html = " 跳到 <select class='selectPage' onchange='window.location=\"?{$http_build_query}&page=\"+this.value'>";
for($i=1; $i<=self::$page_count; $i++){
if($i == self::$current_page){
$html .= "<option value='$i' selected>$i</option>";
}else{
$html .= "<option value='$i'>$i</option>";
}
}
$html .= "</select> 页 ";
return $html;
}
}
用起来也是相当的简单的,只要调用初始化函数init()并输入相对应的参数(代码那里有很详细的注释)就可以了。