zoukankan      html  css  js  c++  java
  • 封装分页

    tp 框架 放在ThinkPHP/Library/org/util/

    <?php
    // author wenl

    namespace OrgUtil;

    class Page{

    public $page = 1; //当前页
    public $page_num =3; //每页显示条数

    public $count; //数据总条数
    public $page_total; //总页数
    public $limit = 0; //偏移量

    public function __construct($count, $param = array()){

    if(!empty($param)) $this->setParam($param);

    $this->page = I('get.p') > 1 ? I('get.p') : 1;

    $this->count = $count;

    $this->page_total = ceil($this->count / $this->page_num);

    $this->limit = ($this->page - 1) * $this->page_num;
    }

    public function setParam($param){

    //每页显示条数
    if($param['page_num']){
    $this->page_num = $param['page_num'];
    }

    }

    public function getPage(){

    //起始页
    $start = $this->page - 3;
    if($start < 1) $start = 1;

    //结束页
    $end = $start + 6;
    if($end > $this->page_total) $end = $this->page_total;

    //循环拼接页码数
    $page_str = '';
    for($i = $start; $i<= $end; $i++){
    if($this->page == $i){
    $page_str .= '<span class="current" id="nowPage">'.$i.'</span> ';
    }else{
    $page_str .= '<a href="javascript:void(0)" class="page" opt="'.$i.'">'.$i.'</a> ';
    }
    }

    //上一页
    $prev = $this->page - 1 > 1 ? $this->page - 1 : 1;
    $prev_str = '<a href="javascript:void(0)" class="page" opt="'.$prev.'">上一页</a> ';

    //下一页
    $next = $this->page + 1 > $this->page_total ? $this->page_total : $this->page + 1;
    $next_str = '<a href="javascript:void(0)" class="page" opt="'.$next.'">下一页</a> ';

    //自定义页码数
    $box = '<input type="text" size="2" id="page" onkeyup="go(this)" value="'.$this->page.'"/><a href="javascript:void(0)" class="page" opt="" id="go">GO</a> ';

    $str = ' |总页数:'.$this->page_total;

    $script = '<script>function go(obj){document.getElementById("go").attributes["opt"].nodeValue = obj.value;}</script>';

    //当前页大于1,展示上一页
    if($this->page > 1) $page_str = $prev_str.$page_str;

    //当前页小于总页数,展示下一页
    if($this->page < $this->page_total) $page_str = $page_str.$next_str;

    return $page_str.$box.$str.$script;

    }
    }

  • 相关阅读:
    IOS 获取时间的问题
    JQ 点赞
    截取字符串第一个空格前的字符
    JQ 的一些方法
    匿名函数var a =function(){}和function a(){}的区别
    完美支持中文编程的 Emacs 配置文件 .emacs
    linux find 命令忽略某个或多个子目录的方法
    Linux下如何用date设置时间
    nagios的监控代码!
    shell脚本监控apache进程数和oracle连接数
  • 原文地址:https://www.cnblogs.com/lixiaomingtongxue/p/7886169.html
Copyright © 2011-2022 走看看