首頁 >後端開發 >php教程 >PHP自練項目之數字與文字的分頁效果在函數中實現

PHP自練項目之數字與文字的分頁效果在函數中實現

WBOY
WBOY原創
2016-08-08 09:20:02918瀏覽

<span><span><br>/*</span><span>*
 * 
 * @param $_sql
 * @param $_size
 </span><span>*/</span><span>function</span> _page(<span>$_sql</span>,<span>$_size</span><span>) {
    </span><span>//</span><span>将里面的所有变量取出来,外部可以访问</span><span>global</span><span>$_page</span>,<span>$_pagesize</span>,<span>$_pagenum</span>,<span>$_pageabsolute</span>,<span>$_num</span><span>;
    </span><span>if</span> (<span>isset</span>(<span>$_GET</span>['page'<span>])) {
        </span><span>$_page</span> = <span>$_GET</span>['page'<span>];
        </span><span>if</span> (<span>empty</span>(<span>$_page</span>) || <span>$_page</span> < 0 || !<span>is_numeric</span>(<span>$_page</span><span>)) {
            </span><span>$_page</span> = 1<span>;
        } </span><span>else</span><span> {
            </span><span>$_page</span> = <span>intval</span>(<span>$_page</span><span>);
        }
    } </span><span>else</span><span> {
        </span><span>$_page</span> = 1<span>;
    }
    </span><span>$_pagesize</span> = <span>$_size</span><span>;
    </span><span>$_num</span> = _num_rows(_query(<span>$_sql</span><span>));
    </span><span>if</span> (<span>$_num</span> == 0<span>) {
        </span><span>$_pageabsolute</span> = 1<span>;
    } </span><span>else</span><span> {
        </span><span>$_pageabsolute</span> = <span>ceil</span>(<span>$_num</span> / <span>$_pagesize</span><span>);
    }
    </span><span>if</span> (<span>$_page</span> > <span>$_pageabsolute</span><span>) {
        </span><span>$_page</span> = <span>$_pageabsolute</span><span>;
    }
    </span><span>$_pagenum</span> = (<span>$_page</span> - 1) * <span>$_pagesize</span><span>;
}


</span><span>/*</span><span>*
 * _paging分页函数
 * @param $_type
 * @return 返回分页
 </span><span>*/</span><span>function</span> _paging(<span>$_type</span><span>) {
    </span><span>global</span><span>$_page</span>,<span>$_pageabsolute</span>,<span>$_num</span><span>;
    </span><span>if</span> (<span>$_type</span> == 1<span>) {
        </span><span>echo</span> '<div>'<span>;
        </span><span>echo</span> '<ul>'<span>;
                </span><span>for</span> (<span>$i</span>=0;<span>$i</span><<span>$_pageabsolute</span>;<span>$i</span>++<span>) {
                        </span><span>if</span> (<span>$_page</span> == (<span>$i</span>+1<span>)) {
                            </span><span>echo</span> '<li><a href="blog.php?page='.(<span>$i</span>+1).'">'.(<span>$i</span>+1).'</a></li>'<span>;
                        } </span><span>else</span><span> {
                            </span><span>echo</span> '<li><a href="blog.php?page='.(<span>$i</span>+1).'">'.(<span>$i</span>+1).'</a></li>'<span>;
                        }
                }
        </span><span>echo</span> '</ul>'<span>;
        </span><span>echo</span> '</div>'<span>;
    } </span><span>elseif</span> (<span>$_type</span> == 2<span>) {
        </span><span>echo</span> '<div>'<span>;
        </span><span>echo</span> '<ul>'<span>;
        </span><span>echo</span> '<li>'.<span>$_page</span>.'/'.<span>$_pageabsolute</span>.'页 | </li>'<span>;
        </span><span>echo</span> '<li>共有<strong>'.<span>$_num</span>.'</strong>个会员 | </li>'<span>;
                </span><span>if</span> (<span>$_page</span> == 1<span>) {
                    </span><span>echo</span> '<li>首页 | </li>'<span>;
                    </span><span>echo</span> '<li>上一页 | </li>'<span>;
                } </span><span>else</span><span> {
                    </span><span>echo</span> '<li><a href="'.SCRIPT.'.php">首页</a> | </li>'<span>;
                    </span><span>echo</span> '<li><a href="'.SCRIPT.'.php?page='.(<span>$_page</span>-1).'">上一页</a> | </li>'<span>;
                }
                </span><span>if</span> (<span>$_page</span> == <span>$_pageabsolute</span><span>) {
                    </span><span>echo</span> '<li>下一页 | </li>'<span>;
                    </span><span>echo</span> '<li>尾页</li>'<span>;
                } </span><span>else</span><span> {
                    </span><span>echo</span> '<li><a href="'.SCRIPT.'.php?page='.(<span>$_page</span>+1).'">下一页</a> | </li>'<span>;
                    </span><span>echo</span> '<li><a href="'.SCRIPT.'.php?page='.<span>$_pageabsolute</span>.'">尾页</a></li>'<span>;
                }
        </span><span>echo</span> '</ul>'<span>;
        </span><span>echo</span> '</div>'<span>;
    }
}</span></span>

在核心函數庫包裝以上程式碼glob.funic.php

然後只要在檔案中呼叫以下函數就行:


//_pageing函數調用分頁,1|2,111表示數字分頁,2表示文字分頁
_paging(2);
?>

以上就介紹了PHP自練項目之數字與文字的分頁效果在函數中實現,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn