在这个页面中,点击网贷百科的分页到第三页,行业活动和网贷新闻也跑到了第三页。thinkphp自带的分页类,只能通过复制这个类更改分页标识符来解决…
自己写了一个分页类,可传入分页标识符,这样就在构造方法中通过传入的分页标识符来区别啦。page.class.php:
点击下载源码
<?php
/**
* @link http://blog.feehi.com/
* @author Fee <admin@feehi.com>
* @since 1.0
*/
class page{
private $perPage = 10;//每页显示条数
private $total;//总记录数
private $pageSign = ‘p';//分页标示符
private $page;//array 当前页
private $rollPage = 10;//一共显示多少页
private $url;//除page参数外的其他地址及query_string
private $totalPage;//总页数
/**
*@description 构造方法
*@var int total 总记录数
*@var int perPage 每页显示条数,默认显示10条
*@var string pageSign 分页标示符,默认为p
*/
public function __CONSTRUCT($total,$perPage=0,$pageSign=0)
{
$this -> total = $total;
if($perPage) $this -> perPage = $perPage;
if($pageSign) $this -> pageSign = $pageSign;
}
/**
*@description thinkphp返回limit语句调用此方法,此方法返回如 start,length
*/
public function limitThinkphp()
{
parse_str($_SERVER[‘QUERY_STRING’],$page);
$page[$this -> pageSign]?$start = $page[$this -> pageSign]:$start = 0;
$this -> page = $page;
$start = ($start -1 ) * $this -> perPage;
return ‘ ‘.$start.’,’.$this->perPage;
}
/**
*@description 手写sql语句返回limit语句调用此方法,此方法返回如 limit start,length
*/
public function limit()
{
/*$arr = explode(‘&’,$_SERVER[‘QUERY_STRING’]);
foreach($arr as $k => $v)
{
$arrV = explode(‘=’,$v);
$page[$arrV[0]] = $arrV[1];
}*/
parse_str($_SERVER[‘QUERY_STRING’],$page);
$page[$this -> pageSign]?$start = $page[$this -> pageSign]:$start = 0;
$this -> page = $page;
$start = ($start -1 ) * $this -> perPage;
return ” limit $start,{$this -> perPage}”;
}
/**
@description 返回分页信息
*/
public function show()
{
$this -> totalPage = ceil($this -> total / $this -> perPage);
$this -> url();
//首页
$firstPage = “<a href='”.$this->url.”&”.$this->pageSign.’=1′.”‘>”.’首页’.”</a>”;
//上一页
$prev = $this -> page[$this -> pageSign]?”<a href='”.$this->url.”&”.$this->pageSign.’=’.($this -> page[$this -> pageSign] -1).”‘>”.’上一页’.”</a>”:”;
if($prev === 0) $prev = ”;
//下一页
$next = $this -> page[$this -> pageSign] + 1 > $this -> totalPage?”:”<a href='”.$this->url.”&”.$this->pageSign.’=’.($this -> page[$this -> pageSign] +1).”‘>”.’下一页’.”</a>”;
//末页
$lastPage = “<a href='”.$this->url.”&”.$this->pageSign.’=’.$this -> totalPage.”‘>”.’末页’.”</a>”;
//当前显示
$current = ‘ 共’.$this->total.’条记录 ‘.$this -> page[$this -> pageSign].’/’.$this -> totalPage.’页';
return $firstPage.$prev.$this -> listPage().$next.$lastPage.$current;
}
/**
@description 去除当前分页标识query_string的其他所有url
*/
private function url()
{
$url = $_SERVER[‘REQUEST_URI’].(strpos($_SERVER[‘REQUEST_URI’],’?’)?”:’?’);
//echo $url;
$parse = parse_url($_SERVER[‘REQUEST_URI’]);
//var_dump($parse);
if(isset($parse[‘query’])){
parse_str($parse[‘query’],$params);
unset($params[$this->pageSign]);
$url = $parse[‘path’].’?’.http_build_query($params);
}
$this -> url = $url;
}
/**
@description 返回分页列信息
*/
private function listPage()
{
$halfRollPage = floor($this -> rollPage/2);
$linkPage = ”;
for($i=$halfRollPage;$i>0;$i–){
if($this -> page[$this -> pageSign] – $i > 0){
$linkPage .= “<a href='”.$this->url.”&”.$this->pageSign.’=’.($this -> page[$this -> pageSign] – $i).”‘>”.($this -> page[$this -> pageSign] – $i).”</a>”;
}else{
continue;
}
}
$linkPage .= “<a class=’current’>”.$this -> page[$this -> pageSign].'</a>';
for($i=1;$i<=$halfRollPage;$i++){
if($this -> page[$this -> pageSign] + $i <= $this -> totalPage){
$linkPage .= “<a href='”.$this->url.”&”.$this->pageSign.’=’.($this -> page[$this -> pageSign] + $i).”‘>”.($this -> page[$this -> pageSign] + $i).”</a>”;
}else{
continue;
}
}
return $linkPage;
}
}
使用方法:
在实例化对象时候,第一个参数传入总记录数,第二个参数可选传入每页显示多少条,第三个参数可选传入分页标识符。thinkphp中使用limitThinkphp成员方法返回limit括号内的条件,手写sql直接使用limit成员方法返回limit的sql部分。调用成员方法show返回分页信息
<?php
//error_reporting(0);
include_once “page.class.php”;
//echo $page -> limit();die;
mysql_connect(‘localhost’,’root’,’xxxxxx’);
mysql_query(“set names utf8″);
mysql_select_db(‘blog’);
$resource = mysql_query(“select count(id) c from blog_posts”);
$count = mysql_fetch_assoc($resource);
$page1 = new page($count[‘c’],2,’page1′);
$sql = “select id,post_title from blog_posts {$page1 -> limit()}”;
//echo $sql;die;
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
$result[] = $row;
}
echo ‘<table>';
echo ‘<th><td>ID</td><td>标题</td></th>';
foreach($result as $k => $v){
echo ‘<tr>';
echo ‘<td>’.$v[‘id’].'</td>';
echo ‘<td>’.$v[‘post_title’].'</td>';
echo ‘</tr>';
}
echo ‘</table>';
echo $page1 -> show();
echo ‘<br><br><br>';
$resource = mysql_query(“select count(comment_ID) c from blog_comments”);
$count = mysql_fetch_assoc($resource);
$page2 = new page($count[‘c’],2,’page2′);
$sql = “select comment_ID,comment_content from blog_comments limit {$page2 -> limitThinkphp()}”;
//echo $sql;die;
$res = mysql_query($sql);$result=”;
while($row = mysql_fetch_assoc($res)){
$result[] = $row;
}
echo ‘<table>';
echo ‘<th><td>ID</td><td>评论</td></th>';
foreach($result as $k => $v){
echo ‘<tr>';
echo ‘<td>’.$v[‘comment_ID’].'</td>';
echo ‘<td>’.$v[‘comment_content’].'</td>';
echo ‘</tr>';
}
echo ‘</table>';
echo $page2 -> show();
?>
效果:
点击分页前,都显示第一页。
点击第二个的3分页后,第二个翻页了,但是第一个仍然是第一页。