PHP 通用分页类的简单示例

前端之家收集整理的这篇文章主要介绍了PHP 通用分页类的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP通用分页类感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
写了个PHP的通用分页

使用方法很简单
在实例化对象时只需要给他传个"每页显示记录数"和"总记录数"就可以了

include_once("pages_class.PHP"); 连接db省略.....
得到总记录数在此省略........
$page = new pages("每页显示数","总记录数");
$limit = $page->offset
$page_html = $page->page_html;
$sql ="select * from table where 1 ".$limit;
$result = MysqL_query($sql,$db);
while($rs= MysqL_fetch_array($result)){
print_r($rs);
}
echo $page_html;

如果你想看看这个万能分页类实现的思路,请继续往下看
如果你想发扬拿来主义精神,请点击下载PHP通用分页
PHP通用分页
 
<?PHP
/**
* PHP通用分页类
* author PHPerz.com * $page_size:每页显示记录数
* $rs_total :总记录数
* 语法:
* new pages($page_size,$rs_total)

* 可用返回值:
* $offset (limit便移量: linit 0,20)
* $page_html (分页连接: 首页 上一页 下一页 末页)
*/
class pages{
var $page_size;
var $rs_total;
var $page_total;
var $page;
var $offset;
var $url;
var $pre_page = "page";
var $page_info;
var $page_html;
function __construct($page_size = 20,$rs_total){
$this->page_size = $page_size;
$this->rs_total = $rs_total;
$this->page_total = ceil($rs_total/$page_size);
$this->page = $this->get_page();
$this->offset = $this->get_offset();
$this->url = $this->get_url();
$this->page_info = $this->page_info();
$this->page_html = $this->page_html($this->page_info);
}

//获取当前页码
private function get_page(){
$page = isset($_GET[$this->pre_page])?$this->f_GET($this->pre_page):1;
$page = $page<1?1:$page;
$page = $page>$this->page_total?$this->page_total:$page;

return $page;
}

//获取当前页的记录偏移量
private function get_offset(){
$offset = " limit ".($this->page - 1) * $this->page_size.",".$this->page_size;
return $offset;
}

//生成翻页HTML代码
public function page_html($page_info){
if (!empty($page_info['first'])){
if ($this->page=="" || $this->page==1){
$page_html = "<span class='page_first'>".$page_info['first']."</span>";
}
else {
$page_html = "<span class='page_first'><a href='?".$this->url['first']."'>".$page_info['first']."</a></span>";
}
}//首页

if (!empty($page_info['pro'])){
if ($this->page=="" || $this->page==1){
$page_html .= "<span class='page_pro'>".$page_info['pro']."</span>";
}
else{
$page_html .= "<span class='page_pro'><a href='?".$this->url['pro']."'>".$page_info['pro']."</a></span>";
}
}//上一页

if (!empty($page_info['next'])){
if ($this->page >= $this->page_total){
$page_html .= "<span class='page_next'>".$page_info['next']."</span>";
}
else{
$page_html .= "<span class='page_next'><a href='?".$this->url['next']."'>".$page_info['next']."</a></span>";
}
}//下一页

if (!empty($page_info['last'])){
if ($this->page >= $this->page_total){
$page_html .= "<span class='page_last'>".$page_info['last']."</span>";
}
else{
$page_html .= "<span class='page_last'><a href='?".$this->url['last']."'>".$page_info['last']."</a></span>";
}
}//最后一页

if (!empty($page_info['t_page'])){
$page_html .= "<span class='t_page'>".str_replace("%i%",$this->page_total,$page_info['t_page'])."</span>";
}
if (!empty($page_info['page'])){
$page_html .= "<span class='now_page'>".str_replace("%i%",$this->page,$page_info['page'])."</span>";
}
if (!empty($page_info['r_total'])){
$page_html .= "<span class='r_page'>".str_replace("%i%",$this->rs_total,$page_info['r_total'])."</span>";
}
return $page_html;

}

//获取当前 URL
private function get_url(){
$url_str = $_GET;
$query_string = array();
foreach($url_str as $key=>$value){
if ($key == $this->pre_page){
continue;
}
$str .= $key."=".$value."&";
}//end for
$next_tmp = $this->page + 1;
$pro_tmp = $this->page - 1;
$last = $str.$this->pre_page."=".$this->page_total;
$next = $str.$this->pre_page."=".$next_tmp;
$pro = $str.$this->pre_page."=".$pro_tmp;
$first = $str.$this->pre_page."=1";

$query_string = array("pro"=>$pro,"next"=>$next,"last"=>$last,"first"=>$first);
return $query_string;
}

private function page_info(){
$arr = array(
"first" => "首页","next" => "下一页","pro" => "上一页","last" => "末页","t_page"=> "共 %i% 页","page" => "当前第 %i% 页","r_total"=> "共 %i% 条"
);
return $arr;
}//page_info()

//过滤函数
private function f_Get($GET){
$GET = $_GET[$GET];
return preg_replace("/[^0-9]+/i","",$GET);
}
}
?>
 

猜你在找的PHP相关文章