页面id出现问题的分页
Pagination having issues with page id
如果我在分页中的第=1页中有10个数据,另一个页面(即第=2页)有另外10个数据。在第=3页之后,该页面不包含任何数据。如果我选择上一个按钮,则该页面将进入第-1页、第=2页……如果我按下下一个按钮时,它也将进入第=2页、第+3页等。那么,如果记录不再可用,如何禁用上一个和下一个按键?
<?php
include("config.php");
$start = 0;
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select id,question,correctAnswer,category from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
.......
........
<?php
$prev = $page - 1;
$next = $page + 1;
echo "<a href='?page=$prev'>prev</a> ";
echo " <a href='?page=$next'>next</a> ";
?>
这个怎么样:
<?php
include("config.php");
$start = 0;
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select id,question,correctAnswer,category from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
.......
........
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href='?page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href='?page=$next'>next</a> ";
}
echo $prev;
echo $next;
?>
使用以下代码:
<?php
include("config.php");
$start = 0;
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
/*add these lines*/
$cnt=mysql_query("select count(*) as ct from math") or die(mysql_error());
$data=mysql_fetch_array($cnt);
$total = $data['ct'];
$start = $page * $per_page - $per_page;
$sql="select id,question,correctAnswer,category from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
.......
........
<?php
$prev = $page - 1;
$next = $page + 1;
/*also add these condition*/
if($page > 1)
echo "<a href='?page=$prev'>prev</a> ";
/*also add these condition*/
if($total > ($page*$per_page))
echo " <a href='?page=$next'>next</a> ";
?>
注意:总是使用PDO或mysqli_*,因为在中不赞成使用mysql_*最新的PHP版本。这将是今后的良好做法。
试试这个:
<?php
include("config.php");
$per_page = 5;
$page = (is_numeric($_GET['page']) ? $_GET['page'] : 0);
$sql = "SELECT `id`,`question`,`correctAnswer`,`category` FROM `math` ORDER BY `id`";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT ".$per_page*$page.", {$per_page}";
$result=mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result)) { ?>
.......
........
<?php
$prev = $page - 1;
$next = $page + 1;
echo "<a href='?page=$prev'>prev</a> ";
echo " <a href='?page=$next'>next</a> ";
?>
相关文章:
- 图像滑块中的分页问题
- 在ajax分页视图(cakephp)中显示adsense的问题
- ajax请求上的kendo分页问题
- 打印时每页上的分页符和表头.打印表 CSS 问题
- 引导轮播分页/幻灯片编号问题
- 理解使用 JavaScript 在分页中加载数据的概念时出现问题
- 历史的ajax分页问题.back()
- 页面id出现问题的分页
- Ng-repeat在另一个Ng-repeat中导致分页问题
- jquery分页与css的逻辑问题
- 使用jquery插件进行分页,当加载50k时记录导致整个系统冻结.如何解决这个问题
- 与其他解决方案相比,内置的ng表分页在性能上存在问题
- wordpress的分页问题
- 无限滚动分页javascript实现问题
- 使用Jquery的基本分页问题
- 单击复选框显示/隐藏GridView列,导致GridView分页问题
- jqGrid分页问题
- ASP.NET的Gridview分页问题
- 静态网站中的表分页问题
- AngularJS ng-grid分页问题-按钮没有被禁用