PHP-如何做出google那样的翻页

PHP-如何做出google那样的翻页

晚风撩人 发布于 2017-04-06 字数 140 浏览 1217 回复 1

我想写个翻页程序,一次最多显示10页,如果是前5页那当前页只是页码加亮,如果超过第10/5页,那就向后加一页,向前减一页,保持当前页在中间。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2017-08-27 1 楼

简单的帮你写了一个分页的类,效果和google的一样,我没有将样式封装进去,已便自己容易扩展。不足的地方请提出,再加以完善。

用法如下:

include 'page.php';

$config = array(
'total' => 500, //数据总数(通过查询所得)
'showLinks' => 10, //显示数字连接的个数
'count' => 10, //每页显示的数量
);

$page = new page($config);
$pageInfo = $page->getPageList();

echo "sql: select * from test where xxx limit {$pageInfo['ofset']},{$config['count']}","<br /><br />";
//输出分页
echo $pageInfo['prev_link'],'&nbsp;',$pageInfo['links'],'&nbsp;',$pageInfo['next_link'];
//更多返回值
//echo "<br/><pre>";
//print_r($pageInfo);

page.php :

<?php
/**
* 简单的分页类
* author:yijun.feng
*/
class page {

private $args = array(
'total' => 0, //总记录数
'showLinks' => 10, //显示数字连接的个数
'pageArg' => 'start', //分页参数
'count' => 10 //每页显示的数量
);

private $baseUrl = '';
public function __construct($config = array()) {

if(!empty($config)) $this->setArgs($config);
}

/**
* 设置分页参数
*/
public function setArgs($config) {

foreach($config as $key =>$val) {
if(isset($this->args[$key])) {
$this->args[$key] = $val;
}
}
$this->baseUrl = $_SERVER ['REQUEST_URI'];
return $this;
}

/**
* 获取分页信息
*/
public function getPageList() {

$page = array();

//计算总页数
$pageTotal = ceil($this->args['total']/$this->args['count']);

//获取page参数,计算偏移量
$s = $_GET[$this->args['pageArg']];
$s = $s !='' && $s > 0 ? $s : 0;
$ofset = floor($s/$this->args['count']) * $this->args['count'];
$ofset = $ofset >= ($this->args['total']) ? ($pageTotal-1)* $this->args['count']: $ofset;
$ofset = $ofset < 0 ? 0 :$ofset;

//计算当前页数
$current = ceil($ofset/$this->args['count']) + 1;

//处理当前连接
if(strpos($this->baseUrl,$this->args['pageArg']) !== false) {
$this->baseUrl = preg_replace('|([?&]?start=[^$]+)|i','',$this->baseUrl);
}
$tmp = parse_url($this->baseUrl);
$this->baseUrl .= !isset($tmp['query']) ? "?" : "&";

//计算数字连接的开始与结束
$index = $current - ceil($this->args['showLinks']/2) > 0 ? $current - ceil($this->args['showLinks']/2) : 1 ;
$index = $pageTotal - $index +1 < $this->args['showLinks'] ? $pageTotal - $this->args['showLinks'] +1: $index;
$index = $index != 1 && $this->args['showLinks'] < 4 && $current != $pageTotal ? $index+1 : $index;
$index = $index < 1 ? 1 : $index;

//计算上一页与下一页
$prevNum = $current > 1 ? $ofset - $this->args['count'] : -1;
$nextNum = $current < $pageTotal ? $ofset + $this->args['count'] : -1;

//生成数字连接
for($i =$index;$i < $index +$this->args['showLinks'];$i++) {
if($i >$pageTotal || $pageTotal <= 1) break;
$href = $this->baseUrl . $this->args['pageArg']."=" . (($i-1) * $this->args['count']);
$page['links'] .= $i != $current ? "<a href='{$href}'>{$i}</a>&nbsp;" : $i ."&nbsp;";
}

//生成上一页下一页连接
$page['prev_link'] = $prevNum < 0 ? '' : "<a href="".$this->baseUrl.$this->args['pageArg']."=".$prevNum."">上一页</a>";
$page['next_link'] = $nextNum < 0 ? '' : "<a href="".$this->baseUrl.$this->args['pageArg']."=".$nextNum."">下一页</a>";

//生成首页,末页连接
$page['first_link'] = $current <=1 ? '' : "<a href="".$this->baseUrl.$this->args['pageArg']."=0">首页</a>";
$page['last_link'] = $current >= $pageTotal ? '' : "<a href="".$this->baseUrl.$this->args['pageArg']."=".(($pageTotal - 1) * $this->args['count']) ."">末页</a>";

//其他信息
$page['cur_page'] = $current;
$page['page_total'] = $pageTotal;
$page['ofset'] = $ofset;

return $page;

}
}

?>