Maison > Article > développement back-end > PHP从练项目之数字与文字的分页效果在函数中实现
PHP自练项目之数字与文字的分页效果在函数中实现
<span style="font-size: 16px;"><span style="color: #008000;"><br>/*</span><span style="color: #008000;">* * * @param $_sql * @param $_size </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span> _page(<span style="color: #800080;">$_sql</span>,<span style="color: #800080;">$_size</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;">将里面的所有变量取出来,外部可以访问</span> <span style="color: #0000ff;">global</span> <span style="color: #800080;">$_page</span>,<span style="color: #800080;">$_pagesize</span>,<span style="color: #800080;">$_pagenum</span>,<span style="color: #800080;">$_pageabsolute</span>,<span style="color: #800080;">$_num</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">])) { </span><span style="color: #800080;">$_page</span> = <span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_page</span>) || <span style="color: #800080;">$_page</span> is_numeric</span>(<span style="color: #800080;">$_page</span><span style="color: #000000;">)) { </span><span style="color: #800080;">$_page</span> = 1<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$_page</span> = <span style="color: #008080;">intval</span>(<span style="color: #800080;">$_page</span><span style="color: #000000;">); } } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$_page</span> = 1<span style="color: #000000;">; } </span><span style="color: #800080;">$_pagesize</span> = <span style="color: #800080;">$_size</span><span style="color: #000000;">; </span><span style="color: #800080;">$_num</span> = _num_rows(_query(<span style="color: #800080;">$_sql</span><span style="color: #000000;">)); </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_num</span> == 0<span style="color: #000000;">) { </span><span style="color: #800080;">$_pageabsolute</span> = 1<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$_pageabsolute</span> = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$_num</span> / <span style="color: #800080;">$_pagesize</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span> > <span style="color: #800080;">$_pageabsolute</span><span style="color: #000000;">) { </span><span style="color: #800080;">$_page</span> = <span style="color: #800080;">$_pageabsolute</span><span style="color: #000000;">; } </span><span style="color: #800080;">$_pagenum</span> = (<span style="color: #800080;">$_page</span> - 1) * <span style="color: #800080;">$_pagesize</span><span style="color: #000000;">;}</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * _paging分页函数 * @param $_type * @return 返回分页 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span> _paging(<span style="color: #800080;">$_type</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">global</span> <span style="color: #800080;">$_page</span>,<span style="color: #800080;">$_pageabsolute</span>,<span style="color: #800080;">$_num</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_type</span> == 1<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '<div id="page_num">'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<ul>'<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$_pageabsolute;<span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span> == (<span style="color: #800080;">$i</span>+1<span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> '<li><a href="blog.php?page='.(<span%20style=" color:>$i+1).'" class="selected">'.(<span style="color: #800080;">$i</span>+1).'</a></li>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">echo</span> '<li><a href="blog.php?page='.(<span%20style=" color:>$i+1).'">'.(<span style="color: #800080;">$i</span>+1).'</a></li>'<span style="color: #000000;">; } } </span><span style="color: #0000ff;">echo</span> '</ul>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '</div>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$_type</span> == 2<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '<div id="page_text">'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<ul>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li>'.<span style="color: #800080;">$_page</span>.'/'.<span style="color: #800080;">$_pageabsolute</span>.'页 | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li>共有<strong>'.<span style="color: #800080;">$_num</span>.'</strong>个会员 | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span> == 1<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '<li>首页 | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li>上一页 | </li>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">echo</span> '<li> <a href="'.SCRIPT.'.php">首页</a> | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li> <a href="'.SCRIPT.'.php?page='.(<span%20style=" color:>$_page-1).'">上一页</a> | </li>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span> == <span style="color: #800080;">$_pageabsolute</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '<li>下一页 | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li>尾页</li>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">echo</span> '<li> <a href="'.SCRIPT.'.php?page='.(<span%20style=" color:>$_page+1).'">下一页</a> | </li>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<li><a href="'.SCRIPT.'.php?page='.<span%20style=" color:>$_pageabsolute.'">尾页</a></li>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> '</ul>'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '</div>'<span style="color: #000000;">; }}</span>
在核心函数库包装以上代码glob.funic.php
然后只要在文件中调用以下函数就行:
//_pageing函数调用分页,1|2,1表示数字分页,2表示文本分页
_paging(2);
?>