php教程 PHP源码 PHP 分页类

PHP 分页类

May 25, 2016 pm 04:59 PM

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>
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)