php 自己写的好看的分页类
自己写的一个分页类 ,不是很完整,个别没有做验证,但可以使用,分页效果见文章底部。除了链接数据库的代码没有粘贴上来,其他的都粘贴了。供学习使用~
<?php
/**
* Created by PhpStorm.
* User: Caoxt
* Date: 15-7-3
* Time: 上午10:03
*/
class page {
public $pageSize; //每页显示的文章条数
public $showPage; //页数条显示的条数
public $count; //文章总条数
public $page; //当前页
public $mergyStr; //存储页码的变量
public $skip; //跳转页开关,默认为false关闭 //初始化参数
public function __construct($count, $pageSize = 10, $showPage = 5, $currentPage = 1, $skip = false) {
$this->pageSize = $pageSize;
$this->showPage = $showPage;
$this->count = $count;
$this->page = $this->checkPage($currentPage);
$this->mergyStr = '';
$this->skip = $skip;
} //检查传递的$page的合法性
public function checkPage($currentPage) {
if($currentPage < 1 || empty($currentPage)) {
$currentPage = 1;
}
if($currentPage > $this->totalPages()) {
$currentPage = $this->totalPages();
}
return $currentPage;
} //计算偏移量
public function pageOffset() {
return ($this->showPage-1)/2;
} //计算总页数
public function totalPages() {
return ceil($this->count/$this->pageSize);
} //获取页面URL
public function getPageUrl() {
$CurrentUrl = $_SERVER["REQUEST_URI"];
$arrUrl = parse_url($CurrentUrl);
$urlQuery = $arrUrl["query"]; if($urlQuery){
//print_r($this->page);
$urlQuery = preg_replace("/(^|&)page=/" . $this->page, "", $urlQuery);
$CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl); if($urlQuery){
$CurrentUrl.="&page";
}
else $CurrentUrl.="page"; } else {
$CurrentUrl.="?page";
} return $CurrentUrl;
} //页码显示行
public function GetPagerContent() {
$start = 1;
$end = $this->totalPages();
$this->mergyStr .= "<div class='page'>";
if($this->page > 1) {
$this->mergyStr .= " <a href='".$this->getPageUrl()."="."1'>首页</a>";
$this->mergyStr .= " <a href='".$this->getPageUrl()."=".($this->page-1)."'>上一页</a>";
}else{
$this->mergyStr .= " <span class='disable'>首页</span>";
$this->mergyStr .= " <span class='disable'>上一页</span>";
} if($this->totalPages() > $this->showPage) {
if($this->page > $this->pageoffset()+1) {
$this->mergyStr .= "...";
} if($this->page > $this->pageoffset()) {
$start = $this->page-$this->pageoffset();
$end = $this->totalPages() > $this->page+$this->pageoffset() ? $this->page+$this->pageoffset() : $this->totalPages();
}
else{
$start = 1;
$end = $this->totalPages() > $this->showPage ? $this->showPage : $this->totalPages();
} if($this->page + $this->pageoffset() > $this->totalPages()) {
$start = $start - ($this->page + $this->pageoffset() - $end);
} } for($i=$start; $i<=$end; $i++) {
if($i == $this->page) {
$this->mergyStr .= "<span class='current'>{$i}</span>";
}else{
$this->mergyStr .= " <a href='".$this->getPageUrl()."=".$i."'>{$i}</a>";
} } if($this->totalPages() > $this->showPage && $this->totalPages() > $this->page + $this->pageoffset()) {
$this->mergyStr .= "...";
} if($this->page < $this->totalPages()) {
$this->mergyStr .= " <a href='".$this->getPageUrl()."=".($this->page+1)."'>下一页</a>";
$this->mergyStr .= " <a href='".$this->getPageUrl()."=".$this->totalPages()."'>尾页</a>";
}else{
$this->mergyStr .= " <span class='disable'>下一页</span>";
$this->mergyStr .= " <span class='disable'>尾页</span>";
} $this->mergyStr .= " 共{$this->totalPages()}页"; //显示跳转框
if($this->skip == true) {
$this->mergyStr .= $this->skipNumPage();
} $this->mergyStr .= "</div>"; print_r($this->mergyStr);
} //跳转页
public function skipNumPage() {
$this->mergyStr .= "<form action='' method='get'>";
$this->mergyStr .= "第<input type='text' name='page' style='width:30px; height:25px; border: 1px solid #aaaadd;'>页";
$this->mergyStr .= "<input type='submit' value='查询' style='border: 1px solid #aaaadd; text-decoration: none; padding:4px 10px 4px 10px; ' >";
$this->mergyStr .= "</form>";
} }
上面是类文件的所有代码,以下是调用实例:
<?php
//数据库链接,不写了 //每页显示条数
$pageSize = 10;
//页码行显示的条数
$showPage = 5;
//当前页
$page = isset($_GET['page']) ? $_GET['page'] : 1; //当前页显示的文章
$sql = "select * from follow_trade_1 order by `follow_trade_id` asc limit ".($page-1)*$pageSize.",{$pageSize}";
$query = mysql_query($sql); //计算总条数
$sql_count = mysql_query("select count(*) from follow_trade_1");
$query_count = mysql_fetch_row($sql_count);
$count = $query_count[0]; //实例化分页类
require("page.php");
$p = new page($count, $pageSize, $showPage, $page, true); //table里的文章数据
echo "<table border='1' style='width:400px;'>";
echo "<tr><td>follow_trade_id</td><td>follow_id</td></tr>";
while($rs = mysql_fetch_assoc($query)) {
echo "<tr><td>{$rs['follow_trade_id']}</td><td>{$rs['follow_id']}</td></tr>";
}
echo "</table>"; //读取分页条
$p->GetPagerContent();
样式CSS:
<style>
div.page{text-align: left; margin-top: 10px;}
div.page a{border: 1px solid #aaaadd; text-decoration: none; padding:2px 10px 2px 10px; margin: 2px;}
div.page span.current{border: 1px solid #000099; background-color: #000099; padding: 4px 10px 4px 10px; margin: 2px; color: #fff; font-weight: bold;}
div.page span.disable{border: 1px solid #eee; padding: 2px 5px 2px 5px; margin: 2px; color: #ddd;}
div.page form{display: inline;}
</style>
效果如图所示:

php 自己写的好看的分页类的更多相关文章
- 二十八、CI框架之自己写分页类,符合CI的路径规范
一.参照了CSDN上某个前辈写的一个CI分页类,自己删删改改仿写了一个类似的分页类,代码如下: 二.我们在模型里面写2个数据查询的函数,一个用于查询数据数量,一个用于查询出具体数据 三.我们在控制器里 ...
- Ci 自己的分页类【原创】
这里是自己手写的一个CI分页类的实现 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** ...
- 二十七、CI框架之自己写分页类并加载(写分页还是有难度,搞了一整天)
一.我们写好自己的分页代码,防止library目录中,带构造函数 二.在模型中,添加2个函数,一个是查询数据的条数,第二个是取出数据库中的数据 三.在控制中,写入相应的代码,如下: 四.在界面中,写入 ...
- ***CI分页:为CodeIgniter写的分页类
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- 用PHP写的一个简单的分页类 1.0版
<?php /* 分页类 用于实现对多条数据分页显示 version:1.0 author:Knight E-Mail:S.Knight.Work@gmail.com Date:2013-10- ...
- 自己写的一个ASP.NET服务器控件Repeater和GridView分页类
不墨迹,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- 用PHP写的一个简单的分页类 2.0版
<?php /* 分页类 用于实现对多条数据分页显示 version:2.0 //基于1.0 数据库查询用mysqli实现 author:Knight E-Mail:S.Knight.Work@ ...
- PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...
- PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...
随机推荐
- 在 Windows Azure 网站中配置动态 IP 地址限制
我们最近对 Windows Azure 网站进行了升级,并启用了IIS8的动态 IP 限制模块.现在,开发人员可以为其网站启用并配置动态 IP 限制功能(或简称 DIPR). 可以通过以下链接查看此 ...
- 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/
前言 Android Studio(以下简称AS)在13年I/O大会后放出预览版到现在放出的正式版1.0(PS.今天又更新到1.0.1了)历时一年多了,虽然Google官方推出的Android开发者的 ...
- ios网络学习------8 xml格式数据的请求处理 用代码块封装
#pragma mark 载入xml - (void)loadXML { //获取网络数据. NSLog(@"load xml"); //从webserver载入数据 NSStri ...
- 基于ZooKeeper的Dubbo简单抽样登记中心
一:设备zookeeper 系统环境 Ubuntu 14.04.2 LTS x64 IP : 192.168.1.102 下载zookeeper-3.4.6.tar.gz到文件夹/opt.拉开拉链 m ...
- WebKit的历史项管理
标准定义 关于历史的管理,和HTML页面载入一样,都有其相应的标准.地址例如以下: WhatWG: https://html.spec.whatwg.org/multipage/browsers.ht ...
- Css Rest 方法
在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...
- ##DAY5 UIControl及其子类
##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...
- window.parent与window.opener的区别与使用
window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...
- 下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s="a"+"b"+"c"+"d";
javac 编译可以对字符串常量直接相加的表达式进行优化, 不必要等到运行期去进行加法运算处理, 而是在编译时去掉其中的加号, 直接将其编译成一个这些常量相连的结果.题目中的第一行代码被编译器在编译时 ...
- Java 网络编程(三) 创建和使用URL访问网络上的资源
链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951877.html 创建和使用URL访问网络上的资源 URL(Uniform Reso ...