Home > Backend Development > PHP Tutorial > PHP implements paging display of data, PHP implements paging display_PHP tutorial

PHP implements paging display of data, PHP implements paging display_PHP tutorial

WBOY
Release: 2016-07-12 08:51:34
Original
840 people have browsed it

PHP implements paging display of data, PHP implements paging display

Paging is a frequently used function in background management, and paging display facilitates the management of large amounts of data.

The example code is as follows:

<!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>
Copy after login

When there is a large amount of data, the above method cannot be used.

<?<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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1129316.htmlTechArticlePHP implements paging display of data, php implements paging display. Paging is a frequently used function in background management, and paging display is convenient. Management of large amounts of data. The example code is as follows: !DOCTYPE htm...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template