Maison > php教程 > php手册 > php经典分页

php经典分页

WBOY
Libérer: 2016-06-06 09:53:04
original
1449 Les gens l'ont consulté
<span style="color: #000000">php
</span><span style="color: #0000ff">class</span><span style="color: #000000"> PageView{
    </span><span style="color: #008000">/*</span><span style="color: #008000">*页码*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$pageNo</span> = 1<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*页大小*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$pageSize</span> = 20<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*共多少页*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$pageCount</span> = 0<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*总记录数*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$totalNum</span> = 0<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*偏移量,当前页起始行*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$offSet</span> = 0<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*每页数据*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$pageData</span> = <span style="color: #0000ff">array</span><span style="color: #000000">();
    
    </span><span style="color: #008000">/*</span><span style="color: #008000">*是否有上一页*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$hasPrePage</span> = <span style="color: #0000ff">true</span><span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*是否有下一页*</span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #800080">$hasNextPage</span> = <span style="color: #0000ff">true</span><span style="color: #000000">;
    
    </span><span style="color: #0000ff">public</span> <span style="color: #800080">$pageNoList</span> = <span style="color: #0000ff">array</span><span style="color: #000000">();
    
    </span><span style="color: #0000ff">public</span> <span style="color: #800080">$jsFunction</span> ='jsFunction'<span style="color: #000000">;
    </span><span style="color: #008000">/*</span><span style="color: #008000">*
     * 
     * @param unknown_type $count 总行数
     * @param unknown_type $size 分页大小
     * @param unknown_type $string
     </span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$count</span>=0, <span style="color: #800080">$size</span>=20,<span style="color: #800080">$pageNo</span>=1,<span style="color: #800080">$pageData</span> =<span style="color: #0000ff">array</span>(),<span style="color: #800080">$jsFunction</span>='jsFunction'<span style="color: #000000">){

        </span><span style="color: #800080">$this</span>->totalNum = <span style="color: #800080">$count</span>;<span style="color: #008000">//</span><span style="color: #008000">总记录数</span>
        <span style="color: #800080">$this</span>->pageSize = <span style="color: #800080">$size</span>;<span style="color: #008000">//</span><span style="color: #008000">每页大小</span>
        <span style="color: #800080">$this</span>->pageNo = <span style="color: #800080">$pageNo</span><span style="color: #000000">;
        </span><span style="color: #008000">//</span><span style="color: #008000">计算总页数</span>
        <span style="color: #800080">$this</span>->pageCount = <span style="color: #008080">ceil</span>(<span style="color: #800080">$this</span>->totalNum/<span style="color: #800080">$this</span>-><span style="color: #000000">pageSize);
        </span><span style="color: #800080">$this</span>->pageCount = (<span style="color: #800080">$this</span>->pageCount$this-><span style="color: #000000">pageCount;
        </span><span style="color: #008000">//</span><span style="color: #008000">检查pageNo</span>
        <span style="color: #800080">$this</span>->pageNo = <span style="color: #800080">$this</span>->pageNo == 0 ? 1 : <span style="color: #800080">$this</span>-><span style="color: #000000">pageNo;
        </span><span style="color: #800080">$this</span>->pageNo = <span style="color: #800080">$this</span>->pageNo > <span style="color: #800080">$this</span>->pageCount? <span style="color: #800080">$this</span>->pageCount : <span style="color: #800080">$this</span>-><span style="color: #000000">pageNo;
        
        </span><span style="color: #008000">//</span><span style="color: #008000">计算偏移</span>
        <span style="color: #800080">$this</span>->offset = ( <span style="color: #800080">$this</span>->pageNo - 1 ) * <span style="color: #800080">$this</span>-><span style="color: #000000">pageSize;
        </span><span style="color: #008000">//</span><span style="color: #008000">计算是否有上一页下一页</span>
        <span style="color: #800080">$this</span>->hasPrePage = <span style="color: #800080">$this</span>->pageNo == 1 ?<span style="color: #0000ff">false</span>:<span style="color: #0000ff">true</span><span style="color: #000000">; 

        </span><span style="color: #800080">$this</span>->hasNextPage = <span style="color: #800080">$this</span>->pageNo >= <span style="color: #800080">$this</span>->pageCount ?<span style="color: #0000ff">false</span>:<span style="color: #0000ff">true</span><span style="color: #000000">;
        
        </span><span style="color: #800080">$this</span>->pageData = <span style="color: #800080">$pageData</span><span style="color: #000000">;
        </span><span style="color: #800080">$this</span>->jsFunction = <span style="color: #800080">$jsFunction</span><span style="color: #000000">;
        
    }
    </span><span style="color: #008000">/*</span><span style="color: #008000">*
     * 分页算法
     * @return
     </span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">function</span><span style="color: #000000"> generatePageList(){
        </span><span style="color: #800080">$pageList</span> = <span style="color: #0000ff">array</span><span style="color: #000000">();
        </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageCount ){
            <span style="color: #0000ff">for</span>(<span style="color: #800080">$i</span>=0;<span style="color: #800080">$i</span>$this->pageCount;<span style="color: #800080">$i</span>++<span style="color: #000000">){
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$i</span>+1<span style="color: #000000">);
            }
        }</span><span style="color: #0000ff">else</span><span style="color: #000000">{
            </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageNo ){
                <span style="color: #0000ff">for</span>(<span style="color: #800080">$i</span>=0;<span style="color: #800080">$i</span>$i++<span style="color: #000000">){
                    </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$i</span>+1<span style="color: #000000">);
                }
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,-1<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>-><span style="color: #000000">pageCount);

            }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageNo > <span style="color: #800080">$this</span>->pageCount - 4<span style="color: #000000">){
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,1<span style="color: #000000">);
                
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,-1<span style="color: #000000">);
                </span><span style="color: #0000ff">for</span>(<span style="color: #800080">$i</span>=5;<span style="color: #800080">$i</span>>0;<span style="color: #800080">$i</span>--<span style="color: #000000">){
                    </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>->pageCount - <span style="color: #800080">$i</span>+1<span style="color: #000000">);
                }
            }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageNo > 4 && <span style="color: #800080">$this</span>->pageNo $this->pageCount - 4<span style="color: #000000">){
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,1<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,-1<span style="color: #000000">);
                
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>->pageNo -2<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>->pageNo -1<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>-><span style="color: #000000">pageNo);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>->pageNo + 1<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>->pageNo + 2<span style="color: #000000">);
                
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,-1<span style="color: #000000">);
                </span><span style="color: #008080">array_push</span>(<span style="color: #800080">$pageList</span>,<span style="color: #800080">$this</span>-><span style="color: #000000">pageCount);
                
            }
        }
        </span><span style="color: #0000ff">return</span> <span style="color: #800080">$pageList</span><span style="color: #000000">;
    }

    </span><span style="color: #008000">/*</span><span style="color: #008000">**
     * 创建分页控件
    * @param
    * @return String
    </span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> echoPageAsDiv(){
        </span><span style="color: #800080">$pageList</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">generatePageList();
        
        </span><span style="color: #800080">$pageString</span> ="<div class="pagination"><div class="page-bottom">"<span style="color: #000000">;
    
        </span><span style="color: #0000ff">if</span>(!<span style="color: #0000ff">empty</span>(<span style="color: #800080">$pageList</span><span style="color: #000000">)){
            </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageCount >1<span style="color: #000000">){
                </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>-><span style="color: #000000">hasPrePage){
                    </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> ."$this->jsFunction . "(" . (<span style="color: #800080">$this</span>->pageNo-1) . ")\">上一页"<span style="color: #000000">;
                }
                </span><span style="color: #0000ff">foreach</span> (<span style="color: #800080">$pageList</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$k</span>=><span style="color: #800080">$p</span><span style="color: #000000">){
                    </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>->pageNo == <span style="color: #800080">$p</span><span style="color: #000000">){
                        </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> ."<span class="page-cur">" . <span style="color: #800080">$this</span>->pageNo . "</span>"<span style="color: #000000">;
                        </span><span style="color: #0000ff">continue</span><span style="color: #000000">;
                    }
                    </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$p</span> == -1<span style="color: #000000">){
                        </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> ."<span class="page-break">...</span>"<span style="color: #000000">;
                        </span><span style="color: #0000ff">continue</span><span style="color: #000000">;
                    }
                    </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> ."$this->jsFunction . "(" . <span style="color: #800080">$p</span> . ")\">" . <span style="color: #800080">$p</span> . ""<span style="color: #000000">;
                }
                
                </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$this</span>-><span style="color: #000000">hasNextPage){
                    </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> ."$this->jsFunction . "(" . (<span style="color: #800080">$this</span>->pageNo+1) . ")\">下一页"<span style="color: #000000">;
                }
                
            }
        }
        </span><span style="color: #800080">$pageString</span> = <span style="color: #800080">$pageString</span> .("</div></div>"<span style="color: #000000">);
        </span><span style="color: #0000ff">return</span> <span style="color: #800080">$pageString</span><span style="color: #000000">;
    }
}

</span>?>
Copier après la connexion
<span style="color: #800000"><style type="text/css">
<!--
.pagination </style></span>{<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff"> Tahoma</span>;<span style="color: #ff0000">overflow</span>:<span style="color: #0000ff"> hidden</span>;<span style="color: #ff0000"> padding-top</span>:<span style="color: #0000ff"> 12px</span>;<span style="color: #ff0000"> text-align</span>:<span style="color: #0000ff"> center</span>;}<span style="color: #800000">
.pagination-tab </span>{<span style="color: #ff0000"> margin-bottom</span>:<span style="color: #0000ff"> 20px</span>;}<span style="color: #800000">
.pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip </span>{<span style="color: #ff0000">
    display</span>:<span style="color: #0000ff"> inline-block</span>;<span style="color: #ff0000">font-family</span>:<span style="color: #0000ff"> Tahoma,SimSun,Arial</span>;<span style="color: #ff0000"> height</span>:<span style="color: #0000ff"> 22px</span>;<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">22px</span>;<span style="color: #ff0000"> margin</span>:<span style="color: #0000ff"> 0</span>;<span style="color: #ff0000"> min-width</span>:<span style="color: #0000ff"> 16px</span>;<span style="color: #ff0000">padding</span>:<span style="color: #0000ff"> 0 5px</span>;<span style="color: #ff0000"> text-align</span>:<span style="color: #0000ff"> center</span>;<span style="color: #ff0000"> vertical-align</span>:<span style="color: #0000ff"> top</span>;<span style="color: #ff0000"> white-space</span>:<span style="color: #0000ff"> nowrap</span>;}<span style="color: #800000">
.pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break </span>{<span style="color: #ff0000">
    border</span>:<span style="color: #0000ff"> 1px solid #ed3d83</span>;<span style="color: #ff0000"> color</span>:<span style="color: #0000ff">#e9357d</span>;<span style="color: #ff0000"> font-weight</span>:<span style="color: #0000ff">bold</span>;}<span style="color: #800000">
.pagination a:hover </span>{<span style="color: #ff0000"> border</span>:<span style="color: #0000ff"> 1px solid #ed3d83</span>;<span style="color: #ff0000">text-decoration</span>:<span style="color: #0000ff"> none</span>;<span style="color: #ff0000"> background-color</span>:<span style="color: #0000ff">#f95f9d</span>;<span style="color: #ff0000"> color</span>:<span style="color: #0000ff">#fff</span>;}<span style="color: #800000">
.pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g </span>{<span style="color: #ff0000"> width</span>:<span style="color: #0000ff"> 36px</span>;<span style="color: #ff0000"> background-image</span>:<span style="color: #0000ff"> url(../static/img/page.gif)</span>;}<span style="color: #800000">
.pagination .page-prev </span>{<span style="color: #ff0000"> background-position</span>:<span style="color: #0000ff"> -0px -38px</span>;<span style="color: #ff0000"> padding-left</span>:<span style="color: #0000ff"> 16px</span>;}<span style="color: #800000">
.pagination .page-prev_g </span>{<span style="color: #ff0000"> background-position</span>:<span style="color: #0000ff">0px -59px</span>;<span style="color: #ff0000"> padding-left</span>:<span style="color: #0000ff"> 16px</span>;<span style="color: #ff0000"> color</span>:<span style="color: #0000ff">#cbcbcb</span>;<span style="color: #ff0000"> font-weight</span>:<span style="color: #0000ff">normal</span>;}<span style="color: #800000">
.pagination .page-next </span>{<span style="color: #ff0000"> background-position</span>:<span style="color: #0000ff"> 0px 0px</span>;<span style="color: #ff0000"> padding-right</span>:<span style="color: #0000ff"> 16px</span>;<span style="color: #ff0000"> font-weight</span>:<span style="color: #0000ff">normal</span>;}<span style="color: #800000">
.pagination .page-next_g </span>{<span style="color: #ff0000"> background-position</span>:<span style="color: #0000ff"> -0px -19px</span>;<span style="color: #ff0000"> padding-right</span>:<span style="color: #0000ff"> 16px</span>;<span style="color: #ff0000"> color</span>:<span style="color: #0000ff">#cbcbcb</span>;}<span style="color: #800000">
.pagination .page-cur </span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff"> #f95f9d</span>;<span style="color: #ff0000"> border</span>:<span style="color: #0000ff"> 1px solid #ed3d83</span>;<span style="color: #ff0000">color</span>:<span style="color: #0000ff"> #fff</span>;<span style="color: #ff0000">font-weight</span>:<span style="color: #0000ff"> bold</span>;}<span style="color: #800000">
.pagination .page-break </span>{<span style="color: #ff0000">border</span>:<span style="color: #0000ff"> medium none</span>;<span style="color: #ff0000"> background</span>:<span style="color: #0000ff">none transparent</span>;<span style="color: #ff0000"> color</span>:<span style="color: #0000ff">#333</span>;}<span style="color: #800000">

-->
</span>
Copier après la connexion
<span style="color: #008000">//</span><span style="color: #008000">php调用     </span>
 <span style="color: #800080">$pageNo</span> = <span style="color: #800080">$_GET</span>['pageNo'<span style="color: #000000">];
 </span><span style="color: #0000ff">if</span>(<span style="color: #0000ff">empty</span>(<span style="color: #800080">$pageNo</span><span style="color: #000000">)){
    </span><span style="color: #800080">$pageNo</span> = 1<span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">分页数据</span>
 <span style="color: #800080">$pageData</span> = News::getNewsPage(<span style="color: #800080">$pageNo</span>,<span style="color: #800080">$pageSize</span><span style="color: #000000">);
 </span><span style="color: #008000">//</span><span style="color: #008000">取得总行数</span>
<span style="color: #800080">$count</span> = News::<span style="color: #000000">getNewsCount();
</span><span style="color: #008000">//</span><span style="color: #008000">创建分页器</span>
<span style="color: #800080">$p</span> = <span style="color: #0000ff">new</span> PageView(<span style="color: #800080">$count</span>['0']['TOTAL'],<span style="color: #800080">$pageSize</span>,<span style="color: #800080">$pageNo</span>,<span style="color: #800080">$pageData</span><span style="color: #000000">);
 </span><span style="color: #008000">//</span><span style="color: #008000">生成页码</span>
<span style="color: #800080">$pageViewString</span> = <span style="color: #800080">$p</span>->echoPageAsDiv();
Copier après la connexion
image
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal