Blogger Information
Blog 30
fans 0
comment 0
visits 23493
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作之分页
深海巨兽皮皮虾的博客
Original
985 people have browsed it

    直接上马:

$page = isset($_GET['p'])? $_GET['p']:1;
$page = ($page ==0)? 1 : $page;
$y = 5;
$x = ($page-1)*$y;
$dsn = "mysql:host=localhost;dbname=demo;charset=utf8";
$pdo = new PDO($dsn,'root','root');
$sql = "SELECT * FROM `user1` LIMIT :x,:y ";
$pdoStmt = $pdo->prepare($sql);
$pdoStmt->bindParam('x',$x,PDO::PARAM_INT);
$pdoStmt->bindParam('y',$y,PDO::PARAM_INT);
$pdoStmt->execute();
echo '<h3 align="center">用户信息表</h3>';
echo '<table border="1" cellpadding="5" cellspacing="0" width="70%" align="center">';
echo '<tr bgcolor="#adff2f">';
echo '<th>ID</th><th>name</th><th>email</th>';
echo '</tr>';
foreach($pdoStmt as $row){
    echo '<tr align="center">';
    echo '<td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['email'].'</td>';
    echo '</tr>';
}
echo '</table>';
$sql = "SELECT * FROM `user1`";
$pdoStmt1 = $pdo->prepare($sql);
$pdoStmt1->execute();
//echo $pdoStmt1->rowCount();
$pageC = ceil($pdoStmt1->rowCount()/$y);
$page = ($pageC == $page)? ($page-1):$page;
echo '<h3 align="center">';
echo '<a href="http://www.php.io/demo/29/4.52.php?p=1">首页</a>';
echo '<a href="http://www.php.io/demo/29/4.52.php?p='.($page-1).'">上一页</a>';
for($i=1;$i<=$pageC;$i++){
    echo '<a href="http://www.php.io/demo/29/4.52.php?p='.$i.'">'.$i.'</a>';
}
echo '<a href="http://www.php.io/demo/29/4.52.php?p='.($page+1).'">下一页</a>';
echo '<a href="http://www.php.io/demo/29/4.52.php?p='.$pageC.'">尾页</a>';
echo '</h3>';


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post