這篇文章主要介紹了關於php實現分頁代碼,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
<?phpheader("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"><?phpecho "<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>
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!