首页 » 网站建设 » php评论体系分页技巧_一款供PHP初学者进修的分页类库

php评论体系分页技巧_一款供PHP初学者进修的分页类库

访客 2024-11-14 0

扫一扫用手机浏览

文章目录 [+]

各位朋友大家好!

本日给大家带来的是一款 供PHP初学者学习的分页类库

php评论体系分页技巧_一款供PHP初学者进修的分页类库

分页显示模板,可用变量参数。
当前选中的分页链接模板,未选中的分页链接模板。
显示文本设置,设置分页模板, 设置选等分页模板,设置未选等分页模板。

php评论体系分页技巧_一款供PHP初学者进修的分页类库
(图片来自网络侵删)

由于源码比较长,想要文件版源码的可以评论或者私聊

废话不多说,上源码!

<?php

/

To change this template, choose Tools | Templates

and open the template in the editor.

/

/

分页类

利用办法:

$page = new Page();

$page->init(1000, 20);

$page->setNotActiveTemplate('<span> {a} </span>');

$page->setActiveTemplate('{a}');

echo $page->show();

/

class Page {

/

总条数

/

private $total;

/

每页大小

/

private $pageSize;

/

总页数

/

private $pageNum;

/

当前页

/

private $page;

/

地址

/

private $uri;

/

分页变量

/

private $pageParam;

/

LIMIT XX,XX

/

private $limit;

/

数字分页显示

/

private $listnum = 8;

/

分页显示模板

可用变量参数

{total} 总数据条数

{pagesize} 每页显示条数

{start} 本页开始条数

{end} 本页结束条数

{pagenum} 共有多少页

{frist} 首页

{pre} 上一页

{next} 下一页

{last} 尾页

{list} 数字分页

{goto} 跳转按钮

/

private $template = '<div><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></div>';

/

当前选中的分页链接模板

/

private $activeTemplate = '<li class=\公众active\"大众><a href=\公众javascript:;\"大众>{text}</a></li>';

/

未选中的分页链接模板

/

private $notActiveTemplate = '<li><a href=\"大众{url}\"大众>{text}</a></li>';

/

显示文本设置

/

private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');

/

初始化

@param type $total 总条数

@param type $pageSize 每页大小

@param type $param url附加参数

@param type $pageParam 分页变量

/

public function init($total, $pageSize, $param = '', $pageParam = 'page') {

$this->total = intval($total);

$this->pageSize = intval($pageSize);

$this->pageParam = $pageParam;

$this->uri = $this->geturi($param);

$this->pageNum = ceil($this->total / $this->pageSize);

$this->page = $this->setPage();

$this->limit = $this->setlimit();

}

/

设置分页模板

@param type $template 模板配置

/

public function setTemplate($template) {

$this->template = $template;

}

/

设置选等分页模板

@param type $activeTemplate 模板配置

/

public function setActiveTemplate($activeTemplate) {

$this->activeTemplate = $activeTemplate;

}

/

设置未选等分页模板

@param type $notActiveTemplate 模板配置

/

public function setNotActiveTemplate($notActiveTemplate) {

$this->notActiveTemplate = $notActiveTemplate;

}

/

返回分页

@return type

/

public function show() {

return str_ireplace(array(

'{total}',

'{pagesize}',

'{start}',

'{end}',

'{pagenum}',

'{frist}',

'{pre}',

'{next}',

'{last}',

'{list}',

'{goto}',

), array(

$this->total,

$this->setPageSize(),

$this->star(),

$this->end(),

$this->pageNum,

$this->frist(),

$this->prev(),

$this->next(),

$this->last(),

$this->pagelist(),

$this->gopage(),

), $this->template);

}

/

获取limit起始数

@return type

/

public function getOffset() {

return ($this->page - 1) $this->pageSize;

}

/

设置LIMIT

@return type

/

private function setlimit() {

return \公众limit \"大众 . ($this->page - 1) $this->pageSize . \公众,{$this->pageSize}\公众;

}

/

获取limit

@param type $args

@return type

/

public function __get($args) {

if ($args == \公众limit\公众) {

return $this->limit;

} else {

return null;

}

}

/

初始化当前页

@return int

/

private function setPage() {

if (!empty($_GET[$this->pageParam])) {

if ($_GET[$this->pageParam] > 0) {

if ($_GET[$this->pageParam] > $this->pageNum)

return $this->pageNum;

else

return $_GET[$this->pageParam];

}

}

return 1;

}

/

初始化url

@param type $param

@return string

/

private function geturi($param) {

$url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], \公众?\"大众) ? \"大众\公众 : \"大众?\公众) . $param;

$parse = parse_url($url);

if (isset($parse[\"大众query\"大众])) {

parse_str($parse[\公众query\公众], $params);

unset($params[\"大众page\公众]);

$url = $parse[\公众path\"大众] . \"大众?\"大众 . http_build_query($params);

return $url;

} else {

return $url;

}

}

/

本页开始条数

@return int

/

private function star() {

if ($this->total == 0) {

return 0;

} else {

return ($this->page - 1) $this->pageSize + 1;

}

}

/

本页结束条数

@return type

/

private function end() {

return min($this->page $this->pageSize, $this->total);

}

/

设置当前页大小

@return type

/

private function setPageSize() {

return $this->end() - $this->star() + 1;

}

/

首页

@return type

/

private function frist() {

$html = '';

if ($this->page == 1) {

$html .= $this->replace(\"大众{$this->uri}&page=1\"大众, $this->config['frist'], true);

} else {

$html .= $this->replace(\"大众{$this->uri}&page=1\"大众, $this->config['frist'], false);

}

return $html;

}

/

上一页

@return type

/

private function prev() {

$html = '';

if ($this->page > 1) {

$html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);

} else {

$html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);

}

return $html;

}

/

分页数字列表

@return type

/

private function pagelist() {

$linkpage = \"大众\"大众;

$lastlist = floor($this->listnum / 2);

for ($i = $lastlist; $i >= 1; $i--) {

$page = $this->page - $i;

if ($page >= 1) {

$linkpage .= $this->replace(\"大众{$this->uri}&page={$page}\公众, $page, false);

} else {

continue;

}

}

$linkpage .= $this->replace(\公众{$this->uri}&page={$this->page}\公众, $this->page, true);

for ($i = 1; $i <= $lastlist; $i++) {

$page = $this->page + $i;

if ($page <= $this->pageNum) {

$linkpage .= $this->replace(\"大众{$this->uri}&page={$page}\"大众, $page, false);

} else {

break;

}

}

return $linkpage;

}

/

下一页

@return type

/

private function next() {

$html = '';

if ($this->page < $this->pageNum) {

$html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false);

} else {

$html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true);

}

return $html;

}

/

末了一页

@return type

/

private function last() {

$html = '';

if ($this->page == $this->pageNum) {

$html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true);

} else {

$html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false);

}

return $html;

}

/

跳转按钮

@return string

/

private function gopage() {

$html = '';

$html.=' <input type=\"大众text\"大众 value=\"大众' . $this->page . '\"大众 onkeydown=\"大众javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}\"大众 style=\"大众width:25px;\"大众/><input type=\"大众button\"大众 onclick=\公众javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'\"大众 value=\"大众GO\"大众/>';

return $html;

}

/

模板更换

@param type $replace 更换内容

@param type $result 条件

@return type

/

private function replace($url, $text, $result = true) {

$template = ($result ? $this->activeTemplate : $this->notActiveTemplate);

$html = str_replace('{url}', $url, $template);

$html = str_replace('{text}', $text, $html);

return $html;

}

}

标签:

相关文章

360手机搜索页面打不开,360网页打不开

手机已经成为人们日常生活中不可或缺的工具。360手机作为一款深受用户喜爱的手机品牌,其搜索页面的便捷性和实用性备受关注。近期许多用...

网站建设 2025-04-02 阅读0 评论0

360手机中出现应用搜索,手机应用商店

应用已成为人们生活中不可或缺的一部分。在众多手机品牌中,360手机凭借其强大的应用搜索功能,为用户带来了便捷的生活体验。本文将从3...

网站建设 2025-04-02 阅读0 评论0

360度怎么搜索网页文件,网页怎么搜索

信息量呈爆炸式增长。如何在海量信息中快速找到所需内容,成为广大用户关注的焦点。360度搜索网页文件技术应运而生,它以全新的搜索模式...

网站建设 2025-04-02 阅读0 评论0

360急速怎么换搜索引擎,360搜索下载安装

搜索引擎已成为我们获取信息的重要途径。市面上的搜索引擎种类繁多,如何选择一款适合自己的搜索引擎,成为了许多用户的一大难题。本文将以...

网站建设 2025-04-02 阅读0 评论0