PHP随笔一之分页类

来源:互联网 发布:网络插座接法图解 编辑:程序博客网 时间:2024/06/05 00:14
 原本后台的分页类是实在难用,竟然还耦合了html文件,而且根本不支持分页跳转这一块功能,于是参考了其他一些开源框架的分页类,做了下调整。
 使用方式:在得到分页栏后
 在html页面头部引入
 <link rel="stylesheet" type=" text/css" media="all" href="/css/page.css" />
 在该页面上输出
 <div class="sabrosus">
   <?php echo $this->page;?>
 </div>
 即可获得样式

 主题可以通过查看page.css来更改

 page.css下载地址http://download.csdn.net/download/a7326012009/9383993

 
<?php /** * 分页类 * @author 应杲臻 * @create 2015-07-24 * @example: * $pagination = new Tool_Pagination(100,10); * $pagination->setJumpUrl('/index/test', $urlParam); * $page = $page->show(); * */ class Ccfx_Tool_Pagination {    // 分页栏每页显示的页数 [1] [2] ... [5] 页码数    public $rollPage = 5;        // 页数跳转时要带的参数    public $parameter  ;        // 分页URL地址    public $url     =   '';        // 默认列表每页显示行数    public $listRows = 20;        // 起始行数    public $firstRow;        // 分页总页面数    public $totalPages;        // 总行数    public $totalRows;        // 当前页数    public $nowPage;        // 分页的栏的总页数    public $coolPages;        // 分页显示定制    public $config  = array (            'header' => '条记录',            'prev'   => '上一页',            'next'   => '下一页',            'first'  => '首页',            'last'   => '末页',            'theme'  => '%totalRow% %header% %nowPage%/%totalPage% 页 %upPage%                           %first%  %prePage%  %linkPage%  %downPage% %nextPage% %end%');       // 默认分页变量名    public $varPage;    /**     * 架构函数     * @access public     * @param array $totalRows  总的记录数     * @param array $listRows  每页显示记录数     * @param array $parameter  分页跳转的参数     */    public function __construct($totalRows, $listRows = '')    {        $this->totalRows    =   $totalRows;        $this->varPage      =   'p' ;                if (!empty($listRows)) {            $this->listRows =   intval($listRows);        }        $this->totalPages   =   ceil($this->totalRows / $this->listRows);     //总页数        $this->coolPages    =   ceil($this->totalPages / $this->rollPage);        $this->firstRow     =   $this->listRows * ($this->nowPage - 1);    }        /**     * 设置配置名称     * @param string $name  配置名     * @param string $value 配置参数     */    public function setConfig($name,$value)    {        if (isset($this->config[$name])) {            $this->config[$name]    =   $value;        }    }        /**     * 设置分页参数     * @param int $varPage     */    public function setVarPage($varPage)    {        if (is_string($varPage) && !empty($varPage)) {            $this->varPage = $varPage;        }    }        /**     * 设置当前页     * @param int $nowPage     */    public function setNowPage($nowPage)    {        $nowPage = intval($nowPage);        if ($nowPage < 1) {            $nowPage = 1;        } elseif (!empty($this->totalPages) && $nowPage > $this->totalPages) {            $nowPage = $this->totalPages;           }        $this->nowPage = $nowPage;    }        /**     * 设置跳转页面     * @param string $baseUrl      跳转的页面     * @param array  $param        页面参数     * @param boolen $isParameter  是否开启跳转参数     */    public function setJumpUrl($baseUrl, $param, $isParameter = TRUE)    {        if (is_string($baseUrl) && !empty($baseUrl)) {            $this->url = $baseUrl;        }        $param[$this->varPage] = isset($param[$this->varPage]) ? $param[$this->varPage] : 1;        $this->setNowPage($param[$this->varPage]);        $param[$this->varPage] = '__PAGE__';        foreach ($param as $key => $value) {            $symbol = empty($this->parameter) ? '?' : '&';            $this->parameter .= $symbol . $key . '=' . $value;        }        if (TRUE === $isParameter) {            $this->url .= $this->parameter;        } else {            $this->url .= '?' . $this->varPage . '=' . $param[$this->varPage];        }    }    /**     * 分页显示输出     * @access public     */    public function show()    {        if (0 == $this->totalRows) return '';        $p              =   $this->varPage;        $nowCoolPage    =   ceil($this->nowPage / $this->rollPage);        $url = $this->url;        //上一页        $upRow          =   $this->nowPage - 1;        $downRow        =   $this->nowPage + 1;        if ($upRow > 0) {            $upPage     =   "<a href='".str_replace('__PAGE__', $upRow, $url)."'>" . $this->config['prev'] . "</a>";        } else {            $upPage     =   '';        }                //下一页        if ($downRow <= $this->totalPages) {            $downPage   =   "<a href='".str_replace('__PAGE__', $downRow, $url)."'>" . $this->config['next'] . "</a>";        } else {            $downPage   =   '';        }                //上/下翻整页,跳到首/尾页        if ($nowCoolPage == 1) {            $theFirst   =   '';            $prePage    =   '';        } else {            $preRow     =   $this->nowPage - $this->rollPage;            $prePage    =   "<a href='".str_replace('__PAGE__', $preRow, $url)."'>" . '前五页' . "</a>";            $theFirst   =   "<a href='".str_replace('__PAGE__', 1, $url)."'>" . $this->config['first'] . "</a>";        }        if ($nowCoolPage == $this->coolPages) {            $nextPage   =   '';            $theEnd     =   '';        } else {            $nextRow    =   $this->nowPage + $this->rollPage;            $theEndRow  =   $this->totalPages;            $nextPage   =   "<a href='".str_replace('__PAGE__', $nextRow, $url)."'>" . '下五页' . "</a>";            $theEnd     =   "<a href='".str_replace('__PAGE__', $theEndRow, $url)."'>" . $this->config['last'] . "</a>";        }        //联动页面        $linkPage = "";        for ($i = 1; $i <= $this->rollPage; $i++) {            $page       =   ($nowCoolPage-1) * $this->rollPage + $i;            if ($page != $this->nowPage) {                if ( $page <= $this->totalPages) {                    $linkPage .= "<a href='".str_replace('__PAGE__', $page, $url)."'>" . $page . "</a>";                } else {                    break;                }            } else {                if ($this->totalPages != 1) {                    // 当前页                    $linkPage .= " <span class='current'>".$page."</span>";                }            }        }        $theme = array(                '%header%',                '%nowPage%',                '%totalRow%',                '%totalPage%',                '%upPage%',                '%downPage%',                '%first%',                '%prePage%',                '%linkPage%',                '%nextPage%',                '%end%'        );        $config = array(                $this->config['header'],                $this->nowPage,                $this->totalRows,                $this->totalPages,                $upPage,                $downPage,                $theFirst,                $prePage,                $linkPage,                $nextPage,                $theEnd,        );                $pageStr = str_replace($theme, $config, $this->config['theme']);        return $pageStr;    } }



0 0
原创粉丝点击