search
Homephp教程php手册PHP分页函数

分页|函数

//分页函数

 

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

 



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.