分页显示模块
通过$_GET方式将要显示的页码变量传递过来,网页根据该变量读取相关数据显示出来。
截图如下:
代码如下:
<html>
<title>分页模块</title>
<style type="text/css">
body{
text-align:center;
}
</style>
<body>
<div id="all">
<?php
$current_page = 1;
if(!empty($_GET['page'])){
$current_page = $_GET['page'];
}
$page_size = 5;
$connect = mysql_connect('localhost', 'root', 'root');
mysql_select_db('guestbook', $connect);
$result = mysql_query("select * from guestbook");
$num_rows = mysql_num_rows($result);
$page_count = ceil($num_rows / $page_size);
mysql_data_seek($result, $page_size * ($current_page - 1));
?>
<table border="1" width="730" cellspacing="1">
<tr>
<td>姓名</td>
<td>Email</td>
<td>备注</td>
<td>日期</td>
</tr>
<?php
$count = 0;
while (($row = mysql_fetch_array($result)) && ($count < $page_size)){
?>
<tr>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td><?php echo $row[3] ?></td>
<td><?php echo $row[4] ?></td>
</tr>
<?php
$count++;
}
mysql_free_result($result);
?>
</table>
<hr size ="2" width="80%"/>
<?php
$pre_page = ($current_page - 1 == 0 )? $current_page : ($current_page - 1);
echo "<a href='index.php?page=$pre_page'>"."上一页"."</a> ";
for ($i = 1; $i <= $page_count; $i++){
echo "<a href = 'index.php?page=$i'>".$i."</a> ";
}
$after_page = ($current_page + 1) > $page_count?$current_page:($current_page + 1);
echo "<a href='index.php?page=$after_page'>"."下一页"."</a> ";
?>
</div>
</body>
</html>