欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP分页函数

程序员文章站 2024-01-08 20:41:04
...

分页|函数

//分页函数

function genpage(&$sql,$page_size=20)

{

global $prepage,$nextpage,$pages,$sums; //out param

$page = $_GET["page"]>=1?$_GET["page"]:1;

$eachpage = $page_size;

$pagesql = strstr($sql," from ");

$pagesql = "select count(*) as ids ".$pagesql;

$result = mysql_query($pagesql);

if($rs = mysql_fetch_array($result)) $sums = $rs[0];

$pages = ceil(($sums-0.5)/$eachpage)-1;

$pages = $pages>=0?$pages:0;

$prepage = ($page>0)?$page-1:0;

$nextpage = ($page

$startpos = $page*$eachpage;

$sql .=" limit $startpos,$eachpage ";

}

//显示分页

function showpage()

{

global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;

$startpage = ($page>=$shownum)?$page-$shownum:0;

$endpage = ($page+$shownum

echo "共".($pages+1)."页: ";

if($page>0)echo "第一页";

if($startpage>0)

echo " ... «";

for($i=$startpage;$i

{

if($i==$page) echo " [".($i+1)."] ";

else echo " ".($i+1)." ";

}

if($endpage

echo "» ... ";

if($page

echo "最后页";

}

?>


--------------------------------------------------------------------------------

调用:

PHP代码:-------------------------------------------------------------------------------- $sql = "select * from table1 where 1 order by id desc "; genpage($sql); //只需要正常代码加上这一行就ok。 $result =mysql_query($sql);while($rs = mysql_fetch_array($result)){//display}showpage(); //显示页?>



上一篇:

下一篇: