php分页实现
Posted jay998
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php分页实现相关的知识,希望对你有一定的参考价值。
<?php
header("content-type:text/html;charset=utf8");
include ‘conn.php‘;
//每页显示的数据条数
$pageSise = 2;
$result = $conn->query("select * from message ");
//数据总条数
$totalNum = $result->num_rows;
//总页数
$totalPageCount = ceil($totalNum/$pageSise);
//判断当前是哪一页
$nowPage = isset($_GET[‘page‘]) ? intval($_GET[‘page‘]) : 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[‘user‘].‘|‘.$row[‘title‘].‘<br>‘;
}
?>
<!--分页开始,首先写好分页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=‘".$_SERVER[‘PHP_SELF‘]."?page=1‘>首页</a></li>";
echo "<li><a href=‘".$_SERVER[‘PHP_SELF‘]."?page=".$prev."‘>«</a></li>";
?>
<?php
$page = 5;
for($a=1;$a<=$page;$a++){
echo
"<li><a href=‘".$_SERVER[‘PHP_SELF‘]."?page=".$a."‘>$a</a></li>";
}
?>
<?php
echo "<li><a href=‘".$_SERVER[‘PHP_SELF‘]."?page=".$next."‘>»</a></li>";
echo " <li><a href=‘".$_SERVER[‘PHP_SELF‘]."?page=".$totalPageCount."‘>尾页</a></li>";
?>
</ul>
以上是关于php分页实现的主要内容,如果未能解决你的问题,请参考以下文章