首页 > php教程 > PHP源码 > PHP 分页类

PHP 分页类

PHP中文网
发布: 2016-05-25 16:59:55
原创
1355 人浏览过

PHP 分页类

PHP 分页类

PHP 分页类

分页代码

<?php
/**
 * 分页类
 * @author  xiaojiong & 290747680@qq.com
 * @date 2011-08-17
 * 
 * show(2)  1 ... 62 63 64 65 66 67 68 ... 150
 * 分页样式 
 * #page{font:12px/16px arial}
 * #page span{float:left;margin:0px 3px;}
 * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
 * #page a.now_page,#page a:hover{color:#fff;background:#05c}
*/
 
class Core_Lib_Page
{
    public     $first_row;        //起始行数
 
    public     $list_rows;        //列表每页显示行数
     
    protected  $total_pages;      //总页数
 
    protected  $total_rows;       //总行数
     
    protected  $now_page;         //当前页数
     
    protected  $method  = &#39;defalut&#39;; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式 
     
    protected  $parameter = &#39;&#39;;
     
    protected  $page_name;        //分页参数的名称
     
    protected  $ajax_func_name;
     
    public     $plus = 3;         //分页偏移量
     
    protected  $url;
     
     
    /**
     * 构造函数
     * @param unknown_type $data
     */
    public function __construct($data = array())
    {
        $this->total_rows = $data[&#39;total_rows&#39;];
 
        $this->parameter         = !empty($data[&#39;parameter&#39;]) ? $data[&#39;parameter&#39;] : &#39;&#39;;
        $this->list_rows         = !empty($data[&#39;list_rows&#39;]) && $data[&#39;list_rows&#39;] <= 100 ? $data[&#39;list_rows&#39;] : 15;
        $this->total_pages       = ceil($this->total_rows / $this->list_rows);
        $this->page_name         = !empty($data[&#39;page_name&#39;]) ? $data[&#39;page_name&#39;] : &#39;p&#39;;
        $this->ajax_func_name    = !empty($data[&#39;ajax_func_name&#39;]) ? $data[&#39;ajax_func_name&#39;] : &#39;&#39;;
         
        $this->method           = !empty($data[&#39;method&#39;]) ? $data[&#39;method&#39;] : &#39;&#39;;
         
         
        /* 当前页面 */
        if(!empty($data[&#39;now_page&#39;]))
        {
            $this->now_page = intval($data[&#39;now_page&#39;]);
        }else{
            $this->now_page   = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;
        }
        $this->now_page   = $this->now_page <= 0 ? 1 : $this->now_page;
     
         
        if(!empty($this->total_pages) && $this->now_page > $this->total_pages)
        {
            $this->now_page = $this->total_pages;
        }
        $this->first_row = $this->list_rows * ($this->now_page - 1);
    }   
     
    /**
     * 得到当前连接
     * @param $page
     * @param $text
     * @return string
     */
    protected function _get_link($page,$text)
    {
        switch ($this->method) {
            case &#39;ajax&#39;:
                $parameter = &#39;&#39;;
                if($this->parameter)
                {
                    $parameter = &#39;,&#39;.$this->parameter;
                }
                return &#39;<a onclick="&#39; . $this->ajax_func_name . &#39;(\&#39;&#39; . $page . &#39;\&#39;&#39;.$parameter.&#39;)" href="javascript:void(0)">&#39; . $text . &#39;</a>&#39; . "\n";
            break;
             
            case &#39;html&#39;:
                $url = str_replace(&#39;?&#39;, $page,$this->parameter);
                return &#39;<a href="&#39; .$url . &#39;">&#39; . $text . &#39;</a>&#39; . "\n";
            break;
             
            default:
                return &#39;<a href="&#39; . $this->_get_url($page) . &#39;">&#39; . $text . &#39;</a>&#39; . "\n";
            break;
        }
    }
     
     
    /**
     * 设置当前页面链接
     */
    protected function _set_url()
    {
        $url  =  $_SERVER[&#39;REQUEST_URI&#39;].(strpos($_SERVER[&#39;REQUEST_URI&#39;],&#39;?&#39;)?&#39;&#39;:"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse[&#39;query&#39;])) {
            parse_str($parse[&#39;query&#39;],$params);
            unset($params[$this->page_name]);
            $url   =  $parse[&#39;path&#39;].&#39;?&#39;.http_build_query($params);
        }
        if(!empty($params))
        {
            $url .= &#39;&&#39;;
        }
        $this->url = $url;
    }
     
    /**
     * 得到$page的url
     * @param $page 页面
     * @return string
     */
    protected function _get_url($page)
    {
        if($this->url === NULL)
        {
            $this->_set_url();   
        }
    //  $lable = strpos(&#39;&&#39;, $this->url) === FALSE ? &#39;&#39; : &#39;&&#39;;
        return $this->url . $this->page_name . &#39;=&#39; . $page;
    }
     
     
    /**
     * 得到第一页
     * @return string
     */
    public function first_page($name = &#39;第一页&#39;)
    {
        if($this->now_page > 5)
        {
            return $this->_get_link(&#39;1&#39;, $name);
        }   
        return &#39;&#39;;
    }
     
    /**
     * 最后一页
     * @param $name
     * @return string
     */
    public function last_page($name = &#39;最后一页&#39;)
    {
        if($this->now_page < $this->total_pages - 5)
        {
            return $this->_get_link($this->total_pages, $name);
        }   
        return &#39;&#39;;
    }  
     
    /**
     * 上一页
     * @return string
     */
    public function up_page($name = &#39;上一页&#39;)
    {
        if($this->now_page != 1)
        {
            return $this->_get_link($this->now_page - 1, $name);
        }
        return &#39;&#39;;
    }
     
    /**
     * 下一页
     * @return string
     */
    public function down_page($name = &#39;下一页&#39;)
    {
        if($this->now_page < $this->total_pages)
        {
            return $this->_get_link($this->now_page + 1, $name);
        }
        return &#39;&#39;;
    }
 
    /**
     * 分页样式输出
     * @param $param
     * @return string
     */
    public function show($param = 1)
    {
        if($this->total_rows < 1)
        {
            return &#39;&#39;;
        }
         
        $className = &#39;show_&#39; . $param;
         
        $classNames = get_class_methods($this);
 
        if(in_array($className, $classNames))
        {
            return $this->$className();
        }
        return &#39;&#39;;
    }
     
    protected function show_2()
    {
        if($this->total_pages != 1)
        {
            $return = &#39;&#39;;
            $return .= $this->up_page(&#39;<&#39;);
            for($i = 1;$i<=$this->total_pages;$i++)
            {
                if($i == $this->now_page)
                {
                    $return .= "<a class=&#39;now_page&#39;>$i</a>\n";
                }
                else
                {
                    if($this->now_page-$i>=4 && $i != 1)
                    {
                        $return .="<span class=&#39;pageMore&#39;>...</span>\n";
                        $i = $this->now_page-3;
                    }
                    else
                    {
                        if($i >= $this->now_page+5 && $i != $this->total_pages)
                        {
                            $return .="<span>...</span>\n"; 
                            $i = $this->total_pages;
                        }
                        $return .= $this->_get_link($i, $i) . "\n";
                    }
                }
            }
            $return .= $this->down_page(&#39;>&#39;);
            return $return;
        }
    }
     
    protected function show_1()
    {
        $plus = $this->plus;
        if( $plus + $this->now_page > $this->total_pages)
        {
            $begin = $this->total_pages - $plus * 2;
        }else{
            $begin = $this->now_page - $plus;
        }
         
        $begin = ($begin >= 1) ? $begin : 1;
        $return = &#39;&#39;;
        $return .= $this->first_page();
        $return .= $this->up_page();
        for ($i = $begin; $i <= $begin + $plus * 2;$i++)
        {
            if($i>$this->total_pages)
            {
                break;
            }
            if($i == $this->now_page)
            {
                $return .= "<a class=&#39;now_page&#39;>$i</a>\n";
            }
            else
            {
                $return .= $this->_get_link($i, $i) . "\n";
            }
        }
        $return .= $this->down_page();
        $return .= $this->last_page();
        return $return;
    }
     
    protected function show_3()
    {
        $plus = $this->plus;
        if( $plus + $this->now_page > $this->total_pages)
        {
            $begin = $this->total_pages - $plus * 2;
        }else{
            $begin = $this->now_page - $plus;
        }       
        $begin = ($begin >= 1) ? $begin : 1;
        $return = &#39;总计 &#39; .$this->total_rows. &#39; 个记录分为 &#39; .$this->total_pages. &#39; 页, 当前第 &#39; . $this->now_page . &#39; 页 &#39;;
        $return .= &#39;,每页 &#39;;
        $return .= &#39;<input type="text" value="&#39;.$this->list_rows.&#39;" id="pageSize" size="3"> &#39;;
        $return .= $this->first_page()."\n";
        $return .= $this->up_page()."\n"; 
        $return .= $this->down_page()."\n";
        $return .= $this->last_page()."\n";
        $return .= &#39;<select onchange="&#39;.$this->ajax_func_name.&#39;(this.value)" id="gotoPage">&#39;;
        
        for ($i = $begin;$i<=$begin+10;$i++)
        {
            if($i>$this->total_pages)
            {
                break;
            }           
            if($i == $this->now_page)
            {
                $return .= &#39;<option selected="true" value="&#39;.$i.&#39;">&#39;.$i.&#39;</option>&#39;;
            }
            else
            {
                $return .= &#39;<option value="&#39; .$i. &#39;">&#39; .$i. &#39;</option>&#39;;
            }           
        }
         $return .= &#39;</select>&#39;;
        return $return;
    }
}
登录后复制

使用方法

###处理html静态化页面分页的情况###
# method 处理环境 设置为 html 
# parameter 为静态页面参数  xxx.com/20-0-0-0-40-?.html 注意问号
# ?问号的位置会自动替换为去向页码 
# now_page 当前页面(静态页面获取不到当前页面所以只有你传入)
$params = array(
            &#39;total_rows&#39;=>100, #(必须)
            &#39;method&#39;    =>&#39;html&#39;, #(必须)
            &#39;parameter&#39; =>&#39;xxx.com/20-0-0-0-40-?.html&#39;,  #(必须)
            &#39;now_page&#39;  =>$_GET[&#39;p&#39;],  #(必须)
            &#39;list_rows&#39; =>10, #(可选) 默认为15
);
$page = new Core_Lib_Page($params);
echo  $page->show(1);
#<a href="xxx.com/20-0-0-0-40-2.html">2</a>
 
 
###处理ajax分页的情况###
# method 处理环境 设置为 ajax
# ajax_func_name ajax分页跳转页面的javascript方法
# parameter    ajax_func_name后面的附带参数 默认为空
# now_page 不到当前页面所以只有你传入
$params = array(
            &#39;total_rows&#39;=>100,
            &#39;method&#39;    =>&#39;ajax&#39;,
            &#39;ajax_func_name&#39; =>&#39;goToPage&#39;,
            &#39;now_page&#39;  =>1,
            #&#39;parameter&#39; =>"&#39;jiong&#39;,&#39;username&#39;",
);
$page = new Core_Lib_Page($params);
echo  $page->show(1);        
#<a href="javascript:void(0)" onclick="goToPage(&#39;7&#39;)">7</a>
#添加了parameter<a href="javascript:void(0)" onclick="goToPage(&#39;6&#39;,&#39;jiong&#39;,&#39;username&#39;)">6</a>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板