- 分享
- 0
- 人气
- 0
- 主题
- 0
- 帖子
- 16
- UID
- 151703
- 积分
- 16
- 阅读权限
- 11
- 注册时间
- 2008-7-1
- 最后登录
- 2008-12-2
- 在线时间
- 14 小时
|
vampcheah
我有php上的问题要请教你
希望你得空回我
<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include 'library/config.php';
include 'library/opendb.php';
// how many rows to show per page
$rowsPerPage = 2;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "SELECT name FROM student LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
// print the random numbers
while($row = mysql_fetch_array($result))
{
echo $row['name'] . '<br>';
}
echo '<br>';
// how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM student";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
// $maxPage = ceil(15/2) = 8
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $numrows . $first . $prev . $nav . $next . $last;
// and close the database connection
include 'library/closedb.php';
?>
</body>
</html>
以上的code的output 是 1 2 3 4 5 6 7 8 9 10 pages 一直加,加到最后一业,可是如果我想把
output 出来的 $nav limit 去 5 个而已应该怎样?
就好像 forum 里或google 的 paging system,show 出来的只有10个页数而
谢谢 |
|