[英]How to show 1 result per page in PHP
我的数据库(MySQL)中有3个餐厅,仅用于测试,我尝试创建一些分页代码,每页向我显示一个餐厅。
所以我实现了一些代码,但是我的问题是每个页面一次又一次显示所有餐厅。
<?php
$page = 1;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
$result = mysqli_query($con,"SELECT COUNT(*) as c FROM restaurants");
$row = mysqli_fetch_assoc($result);
$numberOfPages = $row['c'] / 1;
for ($i = 1; $i <= $numberOfPages; $i++)
{
echo '<a style="margin-left:10px;" href="allrestaurants.php?page='. $i . '">' . $i . '</a>';
}
if (!empty($query))
{
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_assoc($query))
{
$resId=isset($row['id'])?$row['id']:''; ?>
<div class="col-md-4 p-t-30">
<!-- Block1 -->
<div class="blo1">
<?php
$q="SELECT * FROM restaurant_images JOIN restaurants ON restaurant_images.resaurant_id=restaurants.id AND restaurants.id='$resId'";
$queryRestaunt_image=mysqli_query($con,$q);
if (!empty($queryRestaunt_image))
{
if(mysqli_num_rows($queryRestaunt_image)>0)
{
$count=0;
while ($image=mysqli_fetch_assoc($queryRestaunt_image))
{
$count++;
?>
<a href="restaurantdetails.php?id=<?=$resId?>">
<div class="wrap-pic-blo1 bo-rad-10 hov-img-zoom" >
<div style="background:url('./images/restaurants/<?=isset($image['image'])?$image['image']:'intro-01.jpg'?>') no-repeat center; background-size: contain;height:230px; background-color: black">
</div>
</div>
</a>
<?php
if($count==1)break;
}
}
}
?>
我希望每页只有一个结果。
您需要更改SQL查询以正确使用LIMIT
子句:
SELECT * FROM restaurants LIMIT <page offset>, <number of restaurants per page>;
例:
$query = mysqli_query($con, "SELECT * FROM restaurants LIMIT " . ($page - 1) . ", 1");
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
您可以限制为一个,在此处更改:
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 1". ($page * 10 - 10));
然后您必须检查有关分页系统的逻辑...如果使用10 in ($page * 10 - 10)
10-10 ($page * 10 - 10)
的10,因为默认分页为10结果,则必须将10更改为1 in每行...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.