傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
8月232014

PHP自适应分页代码,可以灵活定制CSS样式和分页链接效果!

作者:令狐江   发布:2014-8-23 7:48   分类:php   阅读:2900次   评论:0条  
<?php
/**
 * 可以灵活定制的分页类
 * 
 * 可以定制的选项包括,分页链接显示效果,当前页码链接按钮的样式,URL中获取分页值的名字,可以随意带自己的参数
 * 
 * 使用方法:
 * 1、初始化类的时候需要传入参数,类型为数组。
 * array(
 *  (必填)'totalRows'=>'100', 需要显示的数据的总条数;
 *  (必填)'pageSize'=>'2', 每页需要显示的代码数量;
 *  (必填)'currentPage'=>$_GET['p'], 当前页码,默认可以通过$_GET['p']获取,其中名字p可以定制
 *  (必填)'baseUrl'=>'/welcome?id=3',你当前页面的链接地址,比如为http://www.xxx.com/test.php(或者/test.php),如果后面带有参数则可以为http://www.xxx.com/test?id=8
 *  (选填,默认为3)'offset'=>'3', 当前页码的左右偏移量,比如当前页码为5,则在5的左右各显示几个数字链接,默认为3个,则效果为2,3,4,5,6,7,8
 *  (选填,默认为p)'pageString'=>'p',通过$_GET['p']获取当前页码时候的名字,默认为p
 *  (选填,默认为here)'className'=>'here',当前页码链接按钮的样式,默认样式名为here,所以你可以这样写css样式.here{background:#FF4500;} )
 * 
 * 2、可以使用的方法。
 *  A、初始化类后,需要调用pagination([$style = '1'][,$output=TRUE])方法产生分页链接
 *  关于参数的说明:
 *  @param $style (默认为 1,可不填写) :获取链接全部组件,即 首页+上一页+数字链接+下一页+尾页
 *  @param $style == 2 :仅获取数字链接
 *  @param $style == 3 :仅获取上一页+下一页
 *  @param $style == 4 :仅获取上一页+数字链接+下一页,(不包含首尾页)
 *  
 *  @param $output (默认为TRUE),返回分页链接
 *  @param $output 为FALSE时,直接输出分页链接
 *  
 *  B、getCurrentPage()获取当前页码,经过真伪判断后的,防止用户自行输入错误,比如http://www.xxx.com/test?p=-100;此时通过此方法获取当前页码为1
 *  
 *  C、pageAmount()获取总的页码数量
 *  
 * @author 星空幻颖
 * @link http://blog.sina.com.cn/yanyinghq
 *
 */
class Page
{
    private $pageSize; //您的网站每一页显示的列表条数
    private $totalRows; //通过数据库查询返回的总的记录条数
    private $url; //基准URL
    private $pageAmount; //页码的总数
    private $currentPage; //当前的页码
    private $offset = 4; //页码偏移量
    private $pageString = 'p'; //页码在URL中的名字
    private $classHere = 'class="here"'; //当前页链接的class样式类名,默认为here
     
    //初始化当前页码,记录总条数,每页多少条记录
    public function __construct($param)
    {
        $this->pageSize = $param['pageSize'];
        $this->totalRows = $param['totalRows'];
        $this->url = $param['baseUrl'];
        $this->offset = !empty($param['offset'])?$param['offset']:$this->offset;
        $this->pageString =  !empty($param['pageString'])?$param['pageString']:$this->pageString;
        $this->classHere = !empty($param['className'])?$param['className']:$this->classHere;
        $this->currentPage = (int)$param['currentPage'];
    }
     
    /**
     * 创建分页链接
     * 
     * @param $style 默认为 1 :获取链接全部组件
     * @param $style == 2 :仅获取数字链接
     * @param $style == 3 :仅获取上一页,下一页
     * @param $style == 4 :仅获取上一页、下一页、数字链接,不包含首尾页
     * 
     * @param $output 为TRUE时,返回分页链接
     * @param $output 为FALSE时,直接输出分页链接
     * 
     */
    public function pagination($style = '1',$output=TRUE)
    {
        $this->baseUrl();
        $this->pageAmount();
        $this->currentPage();
             
        //获取全部组件
        if($style == '1')
        {
            $page = $this->indexPage().$this->prevPage().$this->pageNumber().$this->nextPage().$this->endPage();
        }
        else if($style == '2')
        {
            //获取纯数字链接
            $page = $this->pageNumber();
        }
        else if($style == '3')
        {
            //只获取上一页下一页
            $page = $this->prevPage().$this->nextPage();
        }
        else if($style =='4')
        {
            //上一页、下一页、数字链接
            $page = $this->prevPage().$this->pageNumber().$this->nextPage();
        }
         
        if($output)
        {
            return $page;
        }
        else
        {
            echo $page;
        }
    }
     
    /**
     * 获取当前页码
     * 
     * @return 当前页码,经过真伪判断的
     */
    public function getCurrentPage()
    {
        $this->pageAmount();
        $this->currentPage();
        return $this->currentPage;
    }
     
    /**
     * 计算出所有的页数
     * 
     * 可以类外面直接调用此方法返回页码总数
     * 
     * @return 页码的总数
     */
    public function pageAmount()
    {
        $this->pageAmount = ceil( $this->totalRows / $this->pageSize);
        if($this->pageAmount <= 0)
        {
            $this->pageAmount = '1';
        }
        return $this->pageAmount;
    }
     
    /**
     * 判断基准链接是否携带参数
     * 
     * 基准链接为用户提交当前页码链接
     * 
     * 如果携带参数,则在链接之后加&p=
     * 
     * 如果不携带参数,则直接加?p=
     */
    private function baseUrl()
    {
        if(preg_match('/\?/', $this->url))
        {
            $this->url = $this->url.'&'.$this->pageString.'=';
        }
        else
        {
            $this->url = $this->url.'?'.$this->pageString.'=';
        }
    }
     
    /**
     * 验证当前页码的真伪性
     * 
     * 如果当前页码小于1或者没有,则默认当前页码为1
     * 
     * 如果当前页码大于页码总数,则默认当前页码为页码总数
     * 
     */
    private function currentPage()
    {
        if($this->currentPage < 1 || !$this->currentPage)
        {
            $this->currentPage = 1;
        }
        else if(($this->currentPage > $this->pageAmount))
        {
            $this->currentPage = $this->pageAmount;
        }
    }
     
    /**
     * 首页链接
     */
    private function indexPage()
    {
        if($this->currentPage == 1) return;
        return '<a href="'.$this->url.'1">首页</a>';
    }
     
    /**
     * 尾页链接
     */
    private function endPage()
    {
        if($this->currentPage == $this->pageAmount) return;
        return '<a href="'.$this->url.$this->pageAmount.'">尾页</a>';
    }
     
    /**
     * 上一页
     */
    private function prevPage()
    {
        if($this->currentPage == 1) return;
        return '<a href="'.$this->url.( $this->currentPage - 1 ).'">上一页</a>';
    }
     
    /**
     * 下一页
     */
    private function nextPage()
    {
        if($this->currentPage == $this->pageAmount) return;
        return '<a href="'.$this->url.( $this->currentPage + 1 ).'">下一页</a>';
    }
     
    /**
     * 中间页码的链接
     * 
     */
    private function pageNumber()
    {
        $left ="";
        $right = "";
         
        //如果总记录的条数“大于”所有链接的数量时候
        if($this->pageAmount > ($this->offset * 2 + 1))
        {
            //当前页码距离首页的距离
            $leftNum = $this->currentPage - 1;
             
            //当前页码距离尾页的距离
            $rightNum = $this->pageAmount - $this->currentPage;
             
            //当当前页码距离首页距离不足偏移量offset时候,在右边补齐缺少的小方块
            if( $leftNum < $this->offset)
            {
                //左边的链接
                for($i = $leftNum; $i >= 1 ; $i--)
                {
                    $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';
                }
                 
                //右边的链接
                for($j = 1; $j <= ($this->offset * 2 - $leftNum); $j++)
                {
                    $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
                }
            }
            else if($rightNum < $this->offset)
            {
                //左边的链接
                for($i = ($this->offset * 2 - $rightNum); $i >= 1 ; $i--)
                {
                    $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';
                }
                 
                //右边的链接
                for($j = 1; $j <= $rightNum; $j++)
                {
                    $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
                }
            }
            else
            {
                //当前链接左边的链接
                for($i = $this->offset; $i >= 1 ; $i--)
                {
                    $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>'; 
                }
                 
                //当前链接右边的链接
                for($j = 1; $j <= $this->offset; $j++)
                {
                    $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
                }
            }
 
            return $left.'<a href="'.$this->url.$this->currentPage.'" class="here">'.$this->currentPage.'</a>'.$right;
        }
        else
        {
            $allLink='';
            //当页码总数小于需要显示的链接数量时候,则全部显示出来
            for($j = 1; $j <= $this->pageAmount; $j++)
            {
                 $allLink.='<a href="'.$this->url.$j.'" '.($j == $this->currentPage?$this->classHere:'').'>'.$j.'</a>';
            }
            return $allLink;
        }
    }
 
}
 
调用方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
li{width:100%; overflow:hidden; margin-top:20px; list-style:none;}
a{display:block; height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:10px; padding:3px 5px; line-height:30px; text-decoration:none; color:#666;}
a:hover,a.here{background:#FF4500; border-color:#FF4500; color:#FFF;}
 
</style>
</head>
 
<body>
 
<?php
require_once('/page.php');
 
$param = array('totalRows'=>'100','pageSize'=>'2','currentPage'=>@$_GET['p'],'baseUrl'=>'/page_index.php?id=3');
 
 
$page1 = new Page($param);
$page2 = new Page($param);
$page3 = new Page($param);
$page4 = new Page($param);
$page5 = new Page($param);
 
echo '总记录数:100';
echo '<hr />';
echo '每页记录2条<hr/ >';
echo '当前页码:'.$page1->getCurrentPage().'<hr />';
echo '共计'.$page1->pageAmount().'页<hr />';
echo '<li>'.$page1->pagination().'</li>';
echo '<li>'.$page2->pagination('1').'</li>'; //默认为1,所以和不填写效果一样
echo '<li>'.$page3->pagination('2').'</li>';
echo '<li>'.$page4->pagination('3').'</li>';
echo '<li>'.$page5->pagination('4').'</li>';
?>
</body>
</html>
 



本文固定链接: https://www.fulingjiang.cn/php/44.html

blogger
该日志由 令狐江 于2014-8-23 7:48 Saturday发表在 php 分类下。
版权所有:《傅令江的光影色彩世界》 → 《PHP自适应分页代码,可以灵活定制CSS样式和分页链接效果!》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:
上一篇::PHP邮件发送支持附件
下一篇:php记录日志,达到文件大小,自动新建文件

热门文章

  • 兄弟二周年祭

相关文章

  • 适用于最新版6.2版本
  • php分页类,简单实用的一个
  • sg11解密
  • PHP安装与使用VLD查看opcode代码【PHP安装第三方扩展的方法】
  • smarty 条件选择 if else elseif
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • 添加自签名https证书到centos系统信任的问题
    • Auto JS 文件类操作
    • auto.js 一些简单操作
    • 从Zen-cart里迁移产品数据的方法
    • Centos中查找并替换某个目录下所有文件中的某个字符串
    • 360安全浏览器 打不开 关不掉 卸不了
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5