Home  >  Article  >  Backend Development  >  PHP implements paging code

PHP implements paging code

不言
不言Original
2018-07-14 16:45:571639browse

This article mainly introduces the PHP paging code, which has certain reference value. Now I share it with you. Friends in need can refer to it.

<?php
header("content-type:text/html;charset=utf8");
include &#39;conn.php&#39;;
//每页显示的数据条数
$pageSise = 2;
$result = $conn->query("select * from message ");
//数据总条数
$totalNum = $result->num_rows;
//总页数
$totalPageCount = ceil($totalNum/$pageSise);
//判断当前是哪一页
$nowPage = isset($_GET[&#39;page&#39;]) ? intval($_GET[&#39;page&#39;]) : 1;
//上一页
$prev = ($nowPage - 1 <=0)? 1 : $nowPage-1;
//下一页
$next = ($nowPage+1>=$totalPageCount) ? $totalPageCount : $nowPage +1;
//如果传入的页数大于总页数,则重新给nowpage赋值
if($nowPage>$totalPageCount || $nowPage == 0){
//    echo "error:can not found the page<br>";
$nowPage = $totalPageCount;
}
//偏移量
$offset = ($nowPage -1)*$pageSise;
//sql语句
$sql = "select * from message limit $offset,$pageSise";
$result2 = $conn->query($sql);
//输出页面内容
while($row = $result2->fetch_assoc()){
echo $row[&#39;user&#39;].&#39;|&#39;.$row[&#39;title&#39;].&#39;<br>&#39;;
}
?>
<!--分页开始,首先写好分页html和css-->
<link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 
<ul class="pagination">
<?php
echo "<li><a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=1&#39;>首页</a></li>";
echo "<li><a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$prev."&#39;>&laquo;</a></li>";
?>
 
<?php
$page = 5;
for($a=1;$a<=$page;$a++){
echo
"<li><a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$a."&#39;>$a</a></li>";
}
?>
 
<?php
 
echo "<li><a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$next."&#39;>&raquo;</a></li>";
echo " <li><a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$totalPageCount."&#39;>尾页</a></li>";
?>
</ul>

The above is the entire content of this article. I hope it will be helpful to everyone. Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the problem of high concurrency and large traffic in PHP

PHP generates WeChat with parameters QR code of mini program

The above is the detailed content of PHP implements paging code. For more information, please follow other related articles on the PHP Chinese website!

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