>  기사  >  백엔드 개발  >  PHP实现数据分页显示,php实现分页显示_PHP教程

PHP实现数据分页显示,php实现分页显示_PHP教程

WBOY
WBOY원래의
2016-07-12 08:51:34797검색

PHP实现数据分页显示,php实现分页显示

  分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理。

  实例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
</head>
<body>
<?<span>php 
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
   
    </span><span>mysql_query</span>("set names utf8"<span>);
    </span><span>mysql_select_db</span>("zhiye",<span>$con</span><span>);
    
    </span><span>$pageSize</span> = 1;      <span>//</span><span>每页显示的数量</span>
    <span>$rowCount</span> = 0;      <span>//</span><span>要从数据库中获取</span>
    <span>$pageNow</span> = 1;        <span>//</span><span>当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span>if</span> (!<span>empty</span>(<span>$_GET</span>['pageNow'<span>])){
        </span><span>$pageNow</span> = <span>$_GET</span>['pageNow'<span>];
    }
    
    </span><span>$pageCount</span> = 0;   <span>//</span><span>表示共有多少页</span>
    
    <span>$sql1</span> = "select count(id) from user"<span>;
    </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>);
    
    </span><span>if</span>(<span>$row1</span>=<span>mysql_fetch_row</span>(<span>$res1</span><span>)){
        </span><span>$rowCount</span> = <span>$row1</span>[0<span>];
    }
    
    </span><span>//</span><span>计算共有多少页,ceil取进1</span>
    <span>$pageCount</span> = <span>ceil</span>((<span>$rowCount</span>/<span>$pageSize</span><span>));
    
    </span><span>//</span><span>使用sql语句时,注意有些变量应取出赋值。</span>
    <span>$pre</span> = (<span>$pageNow</span>-1)*<span>$pageSize</span><span>;
    
    </span><span>$sql2</span> = "select * from user limit <span>$pre</span>,<span>$pageSize</span>"<span>;
    </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);</span>
  
   <span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$res2</span><span>)){
        </span><span>echo</span> <span>$row</span>['user_name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['email']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['password']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['tel']."<br>"<span>;
   }
    </span><span>for</span> (<span>$i</span>=1;<span>$i</span><=<span>$pageCount</span>;<span>$i</span>++<span>){
        </span><span>echo</span> "<a href='userList.php?pageNow=<span>$i</span>'><span>$i</span></a> "<span>;
    }
</span>?>
</body>
</html>

   当有大量数据时,就不能使用上述方法。

<?<span>php 
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
   
    </span><span>mysql_query</span>("set names utf8"<span>);
    </span><span>mysql_select_db</span>("zhiye",<span>$con</span><span>);
    
    </span><span>$pageSize</span> = 1;      <span>//</span><span>每页显示的数量</span>
    <span>$rowCount</span> = 0;      <span>//</span><span>要从数据库中获取</span>
    <span>$pageNow</span> = 1;        <span>//</span><span>当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span>if</span> (!<span>empty</span>(<span>$_GET</span>['pageNow'<span>])){
        </span><span>$pageNow</span> = <span>$_GET</span>['pageNow'<span>];
    }
    
    </span><span>$pageCount</span> = 0;   <span>//</span><span>表示共有多少页</span>
    
    <span>$sql1</span> = "select count(id) from user"<span>;
    </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>);
    
    </span><span>if</span>(<span>$row1</span>=<span>mysql_fetch_row</span>(<span>$res1</span><span>)){
        </span><span>$rowCount</span> = <span>$row1</span>[0<span>];
    }
    
    </span><span>//</span><span>计算共有多少页,ceil取进1</span>
    <span>$pageCount</span> = <span>ceil</span>((<span>$rowCount</span>/<span>$pageSize</span><span>));
    
    </span><span>//</span><span>使用sql语句时,注意有些变量应取出赋值。</span>
    <span>$pre</span> = (<span>$pageNow</span>-1)*<span>$pageSize</span><span>;
    
    </span><span>$sql2</span> = "select * from user limit <span>$pre</span>,<span>$pageSize</span>"<span>;
    </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);
    
    </span><span>//</span><span>$sql = "select * from user";
    //$res = mysql_query($sql,$con);</span>
  
   <span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$res2</span><span>)){
        </span><span>echo</span> <span>$row</span>['user_name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['email']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['password']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['tel']."<br>"<span>;
   }
   </span><strong><span>if</span>(<span>$pageNow</span>>1<span>){
       </span><span>$prePage</span> = <span>$pageNow</span>-1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$prePage</span>'>pre</a> "<span>;
   }
   </span><span>if</span>(<span>$pageNow</span><<span>$pageCount</span><span>){
       </span><span>$nextPage</span> = <span>$pageNow</span>+1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$nextPage</span>'>next</a> "<span>;
       </span><span>echo</span> "当前页{<span>$pageNow</span>}/共{<span>$pageCount</span>}页"<span>;
   }
   </span><span>echo</span> "<br/><br/>"<span>;
   </span>?>
 
   <form action="userList.php">
        <input type="text" name="pageNow">
        <input type="submit" value="GO">
   </form></strong>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1129316.htmlTechArticlePHP实现数据分页显示,php实现分页显示 分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理。 实例代码如下: !DOCTYPE htm...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.