我目前正在尝试在我的表格中实现分页,然而,每当我点击页面时,它们都不会显示下一页,即使地址栏上的链接清楚地显示了page=2、page=3等。我想知道我的代码是否有任何错误。谢谢
$totalPerPage = 20;
$size= "50";
$sql = ("SELECT DISTINCT * FROM shoe s
INNER JOIN shoelace sc ON s.cod = sc.cod
WHERE s.shoefit =?");
$stmt = $db->prepare($sql);
$stmt->execute([$size]);
$numberofResults = $stmt->rowCount();
$numberOfPages = ceil($numberofResults/$totalPerPage);
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
$thePageFormula = ($page-1)*$totalPerPage;
$query = ("SELECT DISTINCT * FROM shoe s
INNER JOIN w_shoelace sc ON s.cod = sc.cod
WHERE s.shoefit =?
LIMIT $thePageFormula,$totalPerPage");
$stmt2 = $db->prepare($query);
$stmt2->execute([$size]);
while($row = $stmt2->fetch(PDO::FETCH_ASSOC)){
//echos infromation in table form
echo "<table><tr onclick='javascript:showRow(this);'><td>"
. $row['shoename'] . "</td><td>"
. utf8_encode($row['shoecolor']) . "</td><td>"
<img src='data:image/jpeg;base64,".base64_encode($row['shoeimg'])."'width='50' height='30'/></td><tr>";
echo "</table>";
}
for ($page=1; $page <= $numberOfPages ; $page++){
echo "<div class='pagination'>
<a href='?page=" . $page . "'>" . $page . "</a>
</div>";
} 发布于 2018-12-12 13:37:37
我认为问题出在这个SQL查询中
SELECT DISTINCT * FROM shoe s
INNER JOIN w_shoelace sc ON s.cod = sc.cod
WHERE s.shoefit =?
LIMIT $thePageFormula,$totalPerPage对于分页,您必须始终维护查询的偏移量SQL值,这意味着它应该从哪里开始从数据库获取数据。上面的查询应该如下所示
SELECT DISTINCT * FROM shoe s
INNER JOIN w_shoelace sc ON s.cod = sc.cod
WHERE s.shoefit =?
LIMIT *limit_to_show_per_page*, *offset*如果您开始从数据库获取数据,并且第一页的限制是10,那么您的偏移值必须是0,如果转到第二页,则每页的限制将是固定的,即10,并且您的偏移量将从0更改为10,依此类推。希望你明白这一点。
https://stackoverflow.com/questions/53736150
复制相似问题