php分页类的实现与调用 (自我摘记)
page.class.php
<?php
namespace Component;
class Page { private $total; //数据表中总记录数
private $listRows; //每页显示行数
private $limit;
private $uri;
private $pageNum; //页数
private $config = array('header' => "个记录", "prev" => "上一页", "next" => "下一页", "first" => "首 页", "last" => "尾 页");
private $listNum = 8; /*
* $total
* $listRows
*/ public function __construct($total, $listRows = 10, $pa = "") {
$this->total = $total;
$this->listRows = $listRows;
$this->uri = $this->getUri($pa);
$this->page = !empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum = ceil($this->total / $this->listRows);
$this->limit = $this->setLimit();
} private function setLimit() {
return "Limit " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
} private function getUri($pa) {
$url = $_SERVER["REQUEST_URI"] . (strpos($_SERVER["REQUEST_URI"], '?') ? '' : "?") . $pa;
$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;
} function __get($args) {
if ($args == "limit")
return $this->limit;
else
return null;
} private function start() {
if ($this->total == 0)
return 0;
else
return ($this->page - 1) * $this->listRows + 1;
} private function end() {
return min($this->page * $this->listRows, $this->total);
} private function first() {
$html = "";
if ($this->page == 1)
$html .= '';
else
$html .= " <a href='{$this->uri}&page=1'>{$this->config["first"]}</a> "; return $html;
} private function prev() {
$html = "";
if ($this->page == 1)
$html .= '';
else
$html .= " <a href='{$this->uri}&page=" . ($this->page - 1) . "'>{$this->config["prev"]}</a> "; return $html;
} private function pageList() {
$linkPage = ""; $inum = floor($this->listNum / 2); for ($i = $inum; $i >= 1; $i--) {
$page = $this->page - $i; if ($page < 1)
continue; $linkPage .= " <a href='{$this->uri}&page={$page}'>{$page}</a> ";
} $linkPage .= " {$this->page} "; for ($i = 1; $i <= $inum; $i++) {
$page = $this->page + $i;
if ($page <= $this->pageNum)
$linkPage .= " <a href='{$this->uri}&page={$page}'>{$page}</a> ";
else
break;
} return $linkPage;
} private function next() {
$html = "";
if ($this->page == $this->pageNum)
$html .= '';
else
$html .= " <a href='{$this->uri}&page=" . ($this->page + 1) . "'>{$this->config["next"]}</a> "; return $html;
} private function last() {
$html = "";
if ($this->page == $this->pageNum)
$html .= '';
else
$html .= " <a href='{$this->uri}&page=" . ($this->pageNum) . "'>{$this->config["last"]}</a> "; return $html;
} private function goPage() {
return ' <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" value="' . $this->page . '" style="width:25px"><input type="button" value="跳转" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'"> ';
} function fpage($display = array(0, 1, 2, 3, 4, 5, 6, 7, 8)) {
$html[0] = " 共有<b>{$this->total}</b>{$this->config["header"]} ";
// $html[1] = " 每页显示<b>" . ($this->end() - $this->start() + 1) . "</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 ";
$html[2] = " <b>{$this->page}/{$this->pageNum}</b>页 "; $html[3] = $this->first();
$html[4] = $this->prev();
$html[5] = $this->pageList();
$html[6] = $this->next();
$html[7] = $this->last();
$html[8] = $this->goPage();
$fpage = '';
foreach ($display as $index) {
$fpage .= $html[$index];
} return $fpage;
} }
控制器调用方法:
function serverlist(){
$goods = D(ops_gz);
//总页数计算
$count = $goods->count();
$per = 10;
//实例化分页类
$page = new \Component\page($count,$per);
//拼装sql语句
$sql = "select * from ops_gz ".$page->limit;
$info = $goods->query($sql);
//获取页码列表
$pagelist = $page->fpage();
$this->assign(info,$info);
$this->assign(pagelist,$pagelist);
$this->display();
}
view

php分页类的实现与调用 (自我摘记)的更多相关文章
- php分页类的二种调用方法(转载)
php分页类的二种调用方法 原文地址:http://www.xfcodes.com/php/fenye/25584.htm 导读:php分页类的二种调用用法,ajax调用php分页类,非ajax方式调 ...
- php分页类 可直接调用
<?php /** * 分页类 * @author xyy * 调用分页实例 $subPages=new SubPages(数据总条数);//实例化分页类 * //$subPages->s ...
- php实现的分页类
php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...
- asp.net的快捷实用分页类
KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...
- PHPCMS V9 分页类的修改教程
首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...
- php 简单分页类
/** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- php部分---一个分页类、用法
1.分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- webpy分页类 + 上传类
webpy没有分页类.按照php的思路.自己编了一个.数据库用的是sqlite. class Page(object): '''分页类''' def __init__(self,page_size,d ...
- php分页类代码带分页样式效果(转)
php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...
随机推荐
- wordpress | 网站访问速度优化方案(Avada)
一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...
- java中反射的基本使用
fanShe.java package example5;class fanShe{ /*1.应用在一些通用性比较高的代码中. *2.后面学的框架,大多数都是应用框架来实现的. ...
- 正则验证input输入,要求只能输入正数,小数点后保留两位。
<input type="number" step="1" min="0" onkeyup="this.value= thi ...
- 为什么后台返回的日期我输出处理了在苹果手机里显示NAN?
现象: //结束时间var ent_time ="2018-04-28 09:36:00"alert((Date.parse(new Date(ent_time))));/ ...
- 在IOS端点击数字后会调起系统拨号界面
在IOS端点击数字后会调起系统拨号界面,解决方案: <meta name="format-detection" content="telephone=no" ...
- mqtt使用一
最近做的一个项目用到了mqtt协议,我需要从第三方订阅主题接受消息,还需要自己搭建,mqtt服务器去发布主题.下面就详细介绍一下环境的搭建和使用. 1.mqtt介绍 MQTT是一个基于客户端-服务器的 ...
- Python学习笔记:第3天 字符串的操作
目录 1. python的数据类型 2. int类型的操作 3. bool类型 4. 字符串的操作 5. for循环 1. python的数据类型 int 整数 str 字符串.一般不会用字符串保存大 ...
- Learning notes | Data Analysis: 1.2 data wrangling
| Data Wrangling | # Sort all the data into one file files = ['BeijingPM20100101_20151231.csv','Chen ...
- C语言基础——链表的相关操作
1 #include <stdio.h> #include <malloc.h> #include <string.h> #include <math.h&g ...
- categorical[np.arange(n), y] = 1 IndexError: index 2 is out of bounds for axis 1 with size 2
我的错误的代码是:train_labels = np_utils.to_categorical(train_labels,num_classes = 3) 错误的原因: IndexError: ind ...