Blogger Information
Blog 22
fans 0
comment 0
visits 17739
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0105分页操作
yestrue的博客
Original
701 people have browsed it
<?php

/**
 * 实现分页功能
 */
header("Content-type:text/html; charset=utf-8");
//连接数据库
$pdo = new PDO('mysql:dbname=test','root','root');
$sql = "SELECT id,name,email FROM user ORDER BY id ASC LIMIT :offset,:num";
$pdoStmt = $pdo->prepare($sql);

//获取传递的数据
$page = isset($_GET['page'])?$_GET['page']:1;
// $page = 1;
$offset = ($page-1)*5;
$num = 5;
$pdoStmt->bindParam(':offset',$offset,PDO::PARAM_INT);
$pdoStmt->bindParam(':num',$num,PDO::PARAM_INT);
$pdoStmt->execute();
echo '<h2 align="center">用户信息表</h2>';
echo '<table align="center" border=1 cellspacing="0" width="50%" cellpadding="2">';
echo '<tr bgcolor="skyblue"><th>ID</th><th>姓名</th><th>邮箱</th></tr>';
foreach($pdoStmt as $row){
	echo '<tr align="center">';
	echo '<td>'.$row['id'].'</td>'.'<td>'.$row['name'].'</td>'.'<td>'.$row['email'].'</td>';
	echo '</tr>';
}
$pdoStmt2 = $pdo->prepare("SELECT id,name,email FROM user");
$pdoStmt2->execute();
$totalPage = ceil($pdoStmt2->rowCount()/$num);
echo '</table>';
echo '<h3 align="center">';
echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page=1">&nbsp;&nbsp;首页&nbsp;&nbsp;</a>';
echo '<a style="text-decoration:none; color:';
echo ($page==1)?'grey':'';
echo '" href="http://www.test.cn/180105hw/page.php?page=';
echo ($page ==1)?1:($page-1);
echo '">&nbsp;&nbsp;上一页&nbsp;&nbsp;</a>';

for($i=1;$i<=$totalPage;$i++){
	echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page='.$i.'">&nbsp;&nbsp;'.$i.'&nbsp;&nbsp;</a>';
}
echo '<a style="text-decoration:none; color:';
echo ($page==$totalPage)?'grey':'';
echo ' " href="http://www.test.cn/180105hw/page.php?page=';
echo ($page==$totalPage)?$totalPage:($page+1);
echo '">&nbsp;&nbsp;下一页&nbsp;&nbsp;</a>';
echo '<a style="text-decoration:none" href="http://www.test.cn/180105hw/page.php?page='.$totalPage.'">&nbsp;&nbsp;尾页&nbsp;&nbsp;</a>';
echo '</h3>';

运行结果.png

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