PHP24 自定义分页类

分页类的定义

<?php
/**
 * Class MyPage 分页类
 * @package core
 */
class MyPage
{
    private $totalCount; //数据表中总记录数
    private $pageSize; //每页显示行数
    private $limit;//sql分页limit子句
    private $uri;
    private $totalPage; //页数
    private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");
    private $linkNum=8;//分页相关按钮数目设置
    /*
     * $total
     * $listRows
     */
    public function __construct($totalCount, $pageSize=10, $pageURI=""){
        $this->totalCount=$totalCount;
        $this->pageSize=$pageSize;
        $this->uri=$this->getUri($pageURI);
        $this->pageIndex=!empty($_GET["pageIndex"]) ? $_GET["pageIndex"] : 1;
        $this->totalPage=ceil($this->totalCount/$this->pageSize);
        $this->limit=$this->setLimit();
    }


    private function setLimit(){
        return "Limit ".($this->pageIndex-1)*$this->pageSize.", {$this->pageSize}";
    }

    private function getUri($pageURI){
        $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pageURI;
        $parse=parse_url($url);

        if(isset($parse["query"])){
            parse_str($parse['query'],$params);
            unset($params["pageIndex"]);
            $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->totalCount==0)
            return 0;
        else
            return ($this->pageIndex-1)*$this->pageSize+1;
    }

    private function end(){
        return min($this->pageIndex*$this->pageSize,$this->totalCount);
    }

    private function first(){
        $html = "";
        if($this->pageIndex==1)
            $html.='';
        else
            $html.="  <a href='{$this->uri}&pageIndex=1'>{$this->config["first"]}</a>  ";

        return $html;
    }

    private function prev(){
        $html = "";
        if($this->pageIndex==1)
            $html.='';
        else
            $html.="  <a href='{$this->uri}&pageIndex=".($this->pageIndex-1)."'>{$this->config["prev"]}</a>  ";

        return $html;
    }

    private function pageList(){
        $linkPage="";

        $inum=floor($this->linkNum/2);

        for($i=$inum; $i>=1; $i--){
            $pageIndex=$this->pageIndex-$i;

            if($pageIndex<1)
                continue;

            $linkPage.=" <a href='{$this->uri}&pageIndex={$pageIndex}'>{$pageIndex}</a> ";

        }

        $linkPage.=" {$this->pageIndex} ";


        for($i=1; $i<=$inum; $i++){
            $pageIndex=$this->pageIndex+$i;
            if($pageIndex<=$this->totalPage)
                $linkPage.=" <a href='{$this->uri}&pageIndex={$pageIndex}'>{$pageIndex}</a> ";
            else
                break;
        }

        return $linkPage;
    }

    private function next(){
        $html = "";
        if($this->pageIndex==$this->totalPage)
            $html.='';
        else
            $html.="  <a href='{$this->uri}&pageIndex=".($this->pageIndex+1)."'>{$this->config["next"]}</a>  ";

        return $html;
    }

    private function last(){
        $html = "";
        if($this->pageIndex==$this->totalPage)
            $html.='';
        else
            $html.="  <a href='{$this->uri}&pageIndex=".($this->totalPage)."'>{$this->config["last"]}</a>  ";

        return $html;
    }

    private function goPage(){
        return '  <input type="text" οnkeydοwn="javascript:if(event.keyCode==13){var pageIndex=(this.value>'.$this->totalPage.')?'.$this->totalPage.':this.value;location=\''.$this->uri.'&pageIndex=\'+pageIndex+\'\'}" value="'.$this->pageIndex.'" style="width:25px"><input type="button" value="GO" οnclick="javascript:var pageIndex=(this.previousSibling.value>'.$this->totalPage.')?'.$this->totalPage.':this.previousSibling.value;location=\''.$this->uri.'&pageIndex=\'+pageIndex+\'\'">  ';
    }

    function fpage($display=array(0,1,2,3,4,5,6,7,8)){
        $html[0]="  共有<b>{$this->totalCount}</b>{$this->config["header"]}  ";
        $html[1]="  每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条  ";
        $html[2]="  <b>{$this->pageIndex}/{$this->totalPage}</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;
    }
}

  

  

在控制器中使用分页类

<?php
namespace application\admin\controllers;


use application\admin\models\GbmgrModel;
use core\mybase\Controller;


class PageController extends Controller
{
    public function index()
    {
        $this->display();
    }

    /**分页留言数据*/
    public function listgb()
    {
        $pageIndex = isset($_GET['pageIndex']) ? $_GET['pageIndex'] : 1;
        $pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 10;
        $like = isset($_GET['like']) ? $_GET['like'] : null;
        $gm = new GbmgrModel();
        //获得符合条件的总记录数
        $totalCount = $gm->count($like);
        //获得分页数据
        $list = $gm->list($pageIndex, $pageSize, $like);

        $url = "index.php?g=admin&c=page&a=listgb&pageIndex={pageIndex}";

        $myPage=null;
        if ($totalCount > $pageSize) {//总记录数大于每页显示数,显示分页
            $myPage = new \core\MyPage($totalCount, $pageSize, $pageIndex, $url, 2);
        }


        $this->assign('list',$list);
        $this->assign("myPage",$myPage);

        $this->display();
    }
}

  

视图定义

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page类分页</title>
    <link href="<?php echo __ADMIN_CSS__;?>bootstrap.min.css" rel="stylesheet">
    <!--分页链接标签样式-->
    <style type="text/css">

        * {
            margin: 0;
            padding: 0;
        }

        #page {
            height: 40px;
            padding: 20px 0px;
        }

        #page a {
            display: block;
            float: left;
            margin-right: 10px;
            padding: 2px 12px;
            height: 24px;
            border: 1px #cccccc solid;
            background: #fff;
            text-decoration: none;
            color: #808080;
            font-size: 12px;
            line-height: 20px;
        }

        #page a:hover {
            color: #077ee3;
            border: 1px #077ee3 solid;
        }

        #page a.cur {
            border: none;
            background: #077ee3;
            color: #fff;
        }

        #page p {
            float: left;
            padding: 2px 12px;
            font-size: 12px;
            height: 24px;
            line-height: 20px;
            color: #bbb;
            border: 1px #ccc solid;
            background: #fcfcfc;
            margin-right: 8px;
        }

        #page p.pageRemark {
            border-style: none;
            background: none;
            margin-right: 0px;
            padding: 4px 0px;
            color: #666;
        }

        #page p.pageRemark b {
            color: red;
        }

        #page p.pageEllipsis {
            border-style: none;
            background: none;
            padding: 4px 0px;
            color: #808080;
        }
    </style>

</head>
<body>
<div class="content col-sm-8">
    <table id="selectTable" class="table table-striped">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>邮箱</th>
            <th>电话</th>
            <th>留言</th>
        </tr>

        <?php foreach($this->data['list'] as $value){
                echo "<tr>"."<td>".$value['id'].
                     "</td>"."<td>".$value['uname'].
                     "</td>"."<td>".$value['uemail'].
                     "</td>"."<td>".$value['uphone'].
                     "</td>"."<td>".$value['umessage'].
                     "</td>"."</tr>";
        } ?>

    </table>
    <?php echo $this->data['myPage']->outPages();?>
</div>
</body>
</html>

  

转载于:https://www.cnblogs.com/rask/p/9322454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值