码迷,mamicode.com
首页 > Web开发 > 详细

php分页实现

时间:2018-07-14 13:10:39      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:sql   header   sage   sse   ror   set   val   off   for   

<?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."‘>&laquo;</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."‘>&raquo;</a></li>";
echo " <li><a href=‘".$_SERVER[‘PHP_SELF‘]."?page=".$totalPageCount."‘>尾页</a></li>";
?>
</ul>








php分页实现

标签:sql   header   sage   sse   ror   set   val   off   for   

原文地址:https://www.cnblogs.com/jay998/p/9308924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!