首页 > 开发 > PHP > 正文

PHP分页函数

2024-05-04 22:58:53
字体:
来源:转载
供稿:网友

<?

//分页函数

 

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<$pages)?$page+1:$pages; 

      $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<=$pages)?$page+$shownum:$pages;

  

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

    if($page>0)echo "<a href=$php_self?page=0&$querystring>第一页</a>";

    if($startpage>0)

        echo " ... <b><a href=$php_self?page=".($page-$shownum*2)."&$querystring>&laquo;</a></b>";

    for($i=$startpage;$i<=$endpage;$i++)

    {

        if($i==$page)    echo "&nbsp;<b>[".($i+1)."]</b>&nbsp;";

        else        echo "&nbsp;<a href=$php_self?page=$i&$querystring>".($i+1)."</a>&nbsp;";

    }

    if($endpage<$pages)

        echo "<b><a href=$php_self?page=".($page+$shownum*2)."&$querystring>&raquo;</a></b> ... ";

    if($page<$pages)

        echo "<a href=$php_self?page=$pages&$querystring>最后页</a>";

 

}

?>


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

调用:

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(); //显示页?>

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表