Home >Backend Development >PHP Tutorial >PHP paging function sample code_PHP tutorial

PHP paging function sample code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:32819browse

Code, php paging function.

<?<span php
</span><span /*</span><span 
* Created on 2011-07-28
* Author : LKK , http://lianq.net
* 使用方法:
require_once('mypage.php');
$result=mysql_query("select * from mytable", $myconn);
$total=mysql_num_rows($result);    //取得信息总数
pageDivide($total,10);     //调用分页函数

//数据库操作
$result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
while($row=mysql_fetch_array($result)){
...您的操作
}
echo $pagecon;    //输出分页导航内容
</span><span */</span>

<span if</span>(!<span function_exists</span>("pageDivide"<span )){
</span><span #</span><span $total     信息总数</span><span 
#</span><span $shownu    显示数量,默认20</span><span 
#</span><span $url     本页链接</span>
<span function</span> pageDivide(<span $total</span>,<span $shownu</span>=20,<span $url</span>=''<span ){

</span><span #</span><span $page 当前页码</span><span 
#</span><span $sqlfirst mysql数据库起始项</span><span 
#</span><span $pagecon    分页导航内容</span>
<span global</span> <span $page</span>,<span $sqlfirst</span>,<span $pagecon</span>,<span $_SERVER</span><span ;
</span><span $GLOBALS</span>["shownu"]=<span $shownu</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 else</span> <span $page</span>=1<span ;

</span><span #</span><span 如果$url使用默认,即空值,则赋值为本页URL</span>
<span if</span>(!<span $url</span>){ <span $url</span>=<span $_SERVER</span>["REQUEST_URI"<span ];}

</span><span #</span><span URL分析</span>
<span $parse_url</span>=<span parse_url</span>(<span $url</span><span );
@</span><span $url_query</span>=<span $parse_url</span>["query"];    <span //</span><span 取出在问号?之后内容</span>
<span if</span>(<span $url_query</span><span ){
</span><span $url_query</span>=<span preg_replace</span>("/(&?)(page=<span $page</span>)/","",<span $url_query</span><span );
</span><span $url</span> = <span str_replace</span>(<span $parse_url</span>["query"],<span $url_query</span>,<span $url</span><span );
</span><span if</span>(<span $url_query</span><span ){
</span><span $url</span> .= "&page"<span ;
}</span><span else</span> <span $url</span> .= "page"<span ;
}</span><span else</span> <span $url</span> .= "?page"<span ;

</span><span #</span><span 页码计算</span>
<span $lastpg</span>=<span ceil</span>(<span $total</span>/<span $shownu</span>);    <span //</span><span 最后页,总页数</span>
<span $page</span>=<span min</span>(<span $lastpg</span>,<span $page</span><span );
</span><span $prepg</span>=<span $page</span>-1; <span //</span><span 上一页</span>
<span $nextpg</span>=(<span $page</span>==<span $lastpg</span> ? 0 : <span $page</span>+1); <span //</span><span 下一页</span>
<span $sqlfirst</span>=(<span $page</span>-1)*<span $shownu</span><span ;

</span><span #</span><span 开始分页导航内容</span>
<span $pagecon</span> = "显示第 ".(<span $total</span>?(<span $sqlfirst</span>+1):0)."-".<span min</span>(<span $sqlfirst</span>+<span $shownu</span>,<span $total</span>)." 条记录,共 <B><span $total</span></B> 条记录"<span ;
</span><span if</span>(<span $lastpg</span><=1) <span return</span> <span false</span>;    <span //</span><span 如果只有一页则跳出</span>

<span if</span>(<span $page</span>!=1) <span $pagecon</span> .=" <a href='<span $url</span>=1'>首页</a> "; <span else</span> <span $pagecon</span> .=" 首页 "<span ;
</span><span if</span>(<span $prepg</span>) <span $pagecon</span> .=" <a href='<span $url</span>=<span $prepg</span>'>前页</a> "; <span else</span> <span $pagecon</span> .=" 前页 "<span ;
</span><span if</span>(<span $nextpg</span>) <span $pagecon</span> .=" <a href='<span $url</span>=<span $nextpg</span>'>后页</a> "; <span else</span> <span $pagecon</span> .=" 后页 "<span ;
</span><span if</span>(<span $page</span>!=<span $lastpg</span>) <span $pagecon</span>.=" <a href='<span $url</span>=<span $lastpg</span>'>尾页</a> "; <span else</span> <span $pagecon</span> .=" 尾页 "<span ;

</span><span #</span><span 下拉跳转列表,循环列出所有页码</span>
<span $pagecon</span> .=" 到第 <select name='topage' size='1' onchange='window.location=\"<span $url</span>=\"+this.value'>\n"<span ;
</span><span for</span>(<span $i</span>=1;<span $i</span><=<span $lastpg</span>;<span $i</span>++<span ){
</span><span if</span>(<span $i</span>==<span $page</span>) <span $pagecon</span> .="<option value='<span $i</span>' selected><span $i</span></option>\n"<span ;
</span><span else</span> <span $pagecon</span> .="<option value='<span $i</span>'><span $i</span></option>\n"<span ;
}
</span><span $pagecon</span> .="</select> 页,共 <span $lastpg</span> 页"<span ;

}
}</span><span else</span> <span die</span>('pageDivide()同名函数已经存在!'<span );
</span>?>

Articles you may be interested in:
Entry-level PHP simple paging code
Detailed explanation of PHP paging code (with examples)
An example of PHP simple paging code
PHP paging class with multiple paging methods
A useful php paging class
A simple example of php paging code
A practical php paging class
A fast and easy-to-use php paging class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738541.htmlTechArticlecode, php paging function. ? (! ("pageDivide" pageDivide( , =20, ='' , , , ["shownu"]= ( ( ['page' = ['page' =1 (! ){ = ["REQUEST_URI" = ( = ["query"]; ( = ("/(?)(page= )/","", =...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn