Php 分页存在页面id问题
如果我在分页的第1页有10个数据,另一页有10个数据,即第2页有10个数据。在第3页之后不包含任何数据。如果我选择“上一页”按钮,则该页进入第1页,第2页…..如果我按下“下一页”按钮,它也会像第2页一样继续,page=3,依此类推。如果记录不再可用,如何禁用prev和next按钮Php 分页存在页面id问题,php,javascript,pagination,Php,Javascript,Pagination,如果我在分页的第1页有10个数据,另一页有10个数据,即第2页有10个数据。在第3页之后不包含任何数据。如果我选择“上一页”按钮,则该页进入第1页,第2页…..如果我按下“下一页”按钮,它也会像第2页一样继续,page=3,依此类推。如果记录不再可用,如何禁用prev和next按钮 <?php include("config.php"); $start = 0; $per_page = 5; if(!isset($_GET['page'])){
<?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");
$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> ";
?>
.......
........
这个怎么样:
<?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版本。这将是今后的良好做法
提示:您应该验证并转义所有用户输入,以避免注入。另外,请检查那些已弃用的
mysql.*
函数的手册;对于$num_pages=floor($num_rows/$per_page),这将舍入任何不均匀的数字。感谢您的rpl为Nils。但它会给我这样的错误:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解第1行的“-5,5”附近使用的正确语法,您确定使用的是这段代码吗?因为$per_page为5,而$page默认为0-5*0=0,这无法创建-5。但是,尝试替换$page=(是数字($\u GET['page'])?$\u GET['page']:0);to$page=(是数字($\u GET['page'])&&>0?$\u GET['page']:0);