search
Homephp教程PHP源码php分页实例程序函数

在php中分页是我们开发中必须要用到的东西,但在觉得在php分页比在asp中方便了很多,下面我来给各位同学详细介绍人几个不错的php分页类吧,各位同学可参考。

<script>ec(2);</script>

这是我自己写的一个php分页实例

 

 代码如下 复制代码



    简单的PHP分页程序


    //建立连接数据库
    $linkID=@mysql_connect("itlobo.com","root","") or die("you could notconnect mysql");
    //连接的数据库名称
    @mysql_select_db("ceshi") or die("could not select database!");
    //取得记录总数

    $query="SELECT count(*) FROM user";
    $rs = mysql_query($query);
    $myrows=mysql_fetch_array($rs);
    $numrows=$myrows[0];
    //设定每一页显示的记录数
    $pagesize = 1;
    //计算总页数
    $pages=intval($numrows/$pagesize);
    if($numrows%$pagesize)
    $pages++;
    //设置页数
    if(isset($_POST['page']))
       $page=intval($_POST['page']);
    else{
         $page = 1; //没有页数则显示第一页;
        }
    //计算记录偏移量
    $offset = ($page-1)*$pagesize;
    //读取指定的记录数
    $query1="select * from user limit $offset,$pagesize";
    $rs = mysql_query($query1);
    if($myrows=mysql_fetch_array($rs))
    {
       $i = 0;  
?>


   
       
       
   
            do{
            $i++;
    ?>
   
       
       
   
            }
        while($myrows=mysql_fetch_array($rs));
        echo "
用户名 密码
";
    } 
        $first = 1;
        $prev =$page-1;
        $next = $page+1;
        $last=$pages;
        if($page>1)
        {
            echo "首页";
            echo "上一页";
        }
        if($page         {
            echo "下一页";
            echo "最后一页";
        }
        echo "
共有" .$pages. "页(" .$page. "/" .$pages.")";
        for($i=1; $i             echo "[".$i."]";
        echo "[" .$page. "]";
        for($i=$page+1; $i             echo "[".$i."]";
        echo"
";
    ?>      

上面代码不能重复使用,后来整理了一个类

 代码如下 复制代码

#pages{display:block; text-align:center; overflow:hidden; color:#000; font-size:13px}
#pages a{color:#333; text-decoration:none; font-family:Verdana,Geneva,sans-serif; padding:2px 3px; display:block}
#pages li{float:left; display:inline-block; border:1px solid #999; margin-right:3px; margin-left:3px}
#pages #spages{background:#CCC; font-weight:bold; color:#36C}

PHP分页函数类源码:

 代码如下 复制代码

/*
* 分页模块
*/

interface Page{
public function showpage();
}

class AdminPage implements Page{
/*
* 构造参数
* SQL语句,每页显示数,url
*/
private $sql;
private $pageline; //每页显示多少行数据
private $urlstr;
private $nowpage;
private $totalNum;
private $pageNum;
private $pageStr;// 形成分页字符串

public function __construct($sql,$p){
$this->sql = $sql;
$this->pageline = $p;
$this->urlstr = $_SERVER['SCRIPT_NAME'];
if(empty($_GET['p'])){
$this->nowpage = 1; //初始化当前页数为1
}else if(is_numeric($_GET['p'])){
$this->nowpage = $_GET['p'];
}else{
$e = new Error(6);
$e->show();
}

$s = new Sql();
$this->totalNum = $s->results_exist_num($this->sql);
$this->pageNum = ceil($this->totalNum/$this->pageline);

}
/*
* 形式为:
* 首页| 上一页| 1 | 2 | 3 | 4 | 5 |下一页 | 末页
*
*
*/

public function showpage(){
if($this->pageNum >=1 && $this->nowpage

if( $this->pageNum

$this->pageStr = '

  • 首页
  • ';
    if($this->nowpage!=1){
    $this->pageStr .= '
  • 上一页
  • ';
    }
    for($i=1;$ipageNum;$i++){
    if($i==$this->nowpage){
    $this->pageStr .='
  • '.$i.'
  • ';
    }else{
    $this->pageStr .='
  • '.$i.'
  • ';
    }
    }
    if($this->nowpage !=$this->pageNum){
    $this->pageStr .= '
  • 下一页
  • ';
    }

    $this->pageStr .='

  • 末页
  • ';
    echo $this->pageStr;

    }else if($this->pageNum>5){

    $this->pageStr = '

    • 首页
    • ';
      if($this->nowpage !=1){
      $this->pageStr .= '
    • 上一页
    • ';
      }

      for($i=1;$i if($i==$this->nowpage){
      $this->pageStr .='

    • '.$i.'
    • ';
      }else{
      $this->pageStr .='
    • '.$i.'
    • ';
      }
      }
      if($this->nowpage !=$this->pageNum){
      $this->pageStr .= '
    • 下一页
    • ';
      }

      $this->pageStr .='

    • 末页
    • ';
      echo $this->pageStr;
      }

      }else if($this->nowpage pageNum -2 && $this->nowpage>=5){
      $this->pageStr = '

      • 首页
      • ';
        $this->pageStr .= '
      • 上一页
      • ';
        for($i=$this->nowpage-2;$inowpage+2;$i++){
        if($i==$this->nowpage){
        $this->pageStr .='
      • '.$i.'
      • ';
        }else{
        $this->pageStr .='
      • '.$i.'
      • ';
        }
        }
        $this->pageStr .= '
      • 下一页
      • ';
        $this->pageStr .='
      • 末页
      • ';
        echo $this->pageStr;

        }else if($this->nowpage>=$this->pageNum-3 && $this->nowpage pageNum){

        $this->pageStr = '

        • 首页
        • ';
          $this->pageStr .= '
        • 上一页
        • ';

          for($i=$this->pageNum-4;$ipageNum;$i++){
          if($i==$this->nowpage){
          $this->pageStr .='

        • '.$i.'
        • ';
          }else{
          $this->pageStr .='
        • '.$i.'
        • ';
          }
          }
          if($this->nowpage !=$this->pageNum){
          $this->pageStr .= '
        • 下一页
        • ';
          }

          $this->pageStr .='

        • 末页
        • ';
          echo $this->pageStr;

          }
          }
          }

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools