【问题标题】:Need Help adding Pagination on divs Thank you需要帮助在 div 上添加分页谢谢
【发布时间】:2021-11-04 01:18:48
【问题描述】:

你好,有没有办法从 sql 创建加载的 div 页面? 我想将其限制为每页 6 个

我用过:

$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6"); 限制,我想在下一页显示其他的。

if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
$result = mysqli_query($conn,"SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
                    
}
else
{
$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6");
}           
if($result){                
while($row=mysqli_fetch_array($result)){        
$prodID = $row["ID"];   
echo '<ul class="col-sm-4">';
echo '<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<a href="product-details.php?prodid='.$prodID.'" rel="bookmark" title="'.$row['Product'].'"><img src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" width="150" height="150" /></a>
</a>
                    
<h2><a href="product-details.php?prodid='.$prodID.'" rel="bookmark" title="'.$row['Product'].'">'.$row['Product'].'</a></h2>
<h2>₽ '.$row['Price'].'</h2>
<p>Stock: '.$row['Stock'].'</p>
<p>Category: '.$row['Category'].'</p>
                    
<a href="product-details.php?prodid='.$prodID.'" class="btn btn-default add-to-cart"><i class="fa fa-search"></i>View Details</a>
</div>';        
echo '</ul>';           
}
}
?>

谢谢。

【问题讨论】:

  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • 嗨,@Dharman 先生,您能帮忙制作从查询到分页的结果吗?我不知道从哪里开始,甚至教程都不起作用。急需帮助谢谢先生。
  • 你为什么要ping我?请不要寻求帮助,这很粗鲁。 Stack Overflow 不是帮助台。你不能指望人们在这里提供个人帮助。如果您需要个人指导,我建议您参加编程课程或找导师。
  • 对不起先生。我只是想你知道怎么做。很抱歉给您带来麻烦。

标签: php html mysql


【解决方案1】:

使用带有偏移量的 LIMIT 子句。例如:

 SELECT ... LIMIT 0, 6 -- select the six rows starting at row 0

然后

 SELECT ... LIMIT 6, 6 -- select the six rows starting at row 6
 SELECT ... LIMIT 12, 6 -- select the six rows starting at row 12
 ...

查看MySQL Manual了解更多详情

【讨论】:

  • 嗨,如果显示超过 6 个项目,我希望它在底部有页码(我有很多项目,所以它会是很多页)。我试过你的答案,但它只限制了它们。感谢您的回答。
猜你喜欢
  • 2021-10-16
  • 2019-09-07
  • 2020-07-04
  • 1970-01-01
  • 2019-01-04
  • 2018-10-11
  • 2021-05-29
  • 2014-03-06
  • 1970-01-01
相关资源
最近更新 更多