PDO分頁

代碼:php

<?php
header("content-type:text/html;charset=utf8");
$dns = "mysql:host=localhost;dbname=數據庫";
$db = new PDO($dns,'root','root',array(PDO::ATTR_PERSISTENT));
$db -> query("set names utf-8");
//查詢總條數
$count = $db -> query("SELECT COUNT(*) FROM seven_day") -> fetchColumn();
//當前頁
$page = isset($_GET['page']) ? $_GET['page'] : 1;
//每頁顯示條數
$size = 2;
//總頁數(尾頁)
$last = ceil($count/$size);
//上一頁
$prev_page = $page - 1 < 1 ? 1 : $page - 1;
//下一頁
$next_page = $page + 1 > $last ? $last : $page + 1;
//偏移量
$offset = ($page - 1) * $size;
//執行sql語句
$data = $db -> query("select * from seven_day limit $offset,$size");
?>

html頁面:html

<div>    <a href="show.php?page=<?php echo 1?>">首頁</a>&nbsp;&nbsp;    <a href="show.php?page=<?php echo $prev_page?>">上一頁</a>&nbsp;&nbsp;    <a href="show.php?page=<?php echo $next_page?>">下一頁</a>&nbsp;&nbsp;    <a href="show.php?page=<?php echo $last?>">尾頁</a>&nbsp;&nbsp;</div>
相關文章
相關標籤/搜索