Heim > Backend-Entwicklung > PHP-Tutorial > PHP实现数据分页显示,php实现分页显示_PHP教程

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

WBOY
Freigeben: 2016-07-12 08:51:34
Original
827 Leute haben es durchsucht

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>
Nach dem Login kopieren

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

<?<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>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1129316.htmlTechArticlePHP实现数据分页显示,php实现分页显示 分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理。 实例代码如下: !DOCTYPE htm...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage