我需要从给定范围的数据中从名为 booked 的表中选择数据并显示数据形成一个变量,然后用它从另一个表中选择并显示所选的数据但是当从表中选择的数据已预订是多个只有第一个数据显示在变量中这是我的代码:
$res1=mysqli_query($bd,"select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'");
$num1=mysqli_num_rows($res1);
if($num1>0)
{
for($y=0;$y<$row1=mysqli_fetch_assoc($res1);$y++)
{
$res=mysqli_query($bd,"select * from rooms where capacity>='$newcap' and room_number!='".$row1['roomnumber']."'");
while($row=mysqli_fetch_assoc($res))
{
echo'<div class="col-lg-4 col-md-4 col-sm-12">';
echo'<div class="newsBox">
<div class="thumbnail">
<figure><img src="reservation/img/rooms/'.$row['img'].'" width="230" height="150"></figure>
<div class="caption maxheight2">
<div class="box_inner">
<div class="box">
<a class="title"><strong>'.$row['name'].'</strong></p>
<b>'.$row['description'].'</b>
<p>'.$row['price'].'</p>
</div>
<a class="btn btn-default" href="info_pay.php?roomnumber='.$row['room_number'].'&roomtype='.$row['name'].'&from='.$_POST['from'].'&adult='.$_POST['adult'].'&child='.$_POST['child'].'&to='.$_POST['to'].'&roomprice='.$row['price'].'"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
</div>
</div>
</div>
</div>';
echo'</div>';
}
}
}
最佳答案
您可能想使用 heredoc 尝试不同的方法因为它不太容易出现引用错误,下面是一个使用 heredoc
循环 mysqli
查询的完整示例。
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'";
if ($result=mysqli_query($con,$sql))
{
while ($row=mysqli_fetch_row($result))
{
echo <<< LOL
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="newsBox">
<div class="thumbnail">
<figure><img src="reservation/img/rooms/{$row['img']}" width="230" height="150"></figure>
<div class="caption maxheight2">
<div class="box_inner">
<div class="box">
<a class="title"><strong>{$row['name']}</strong></p>
<b>{$row['description']}</b>
<p>{$row['price']}</p>
</div>
<a class="btn btn-default" href="info_pay.php?roomnumber={$row['room_number']}&roomtype={$row['name']}&from={$_POST['from']}&adult={$_POST['adult']}&child={$_POST['child']}&to={$_POST['to']}&roomprice={$row['price']}"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
</div>
</div>
</div>
</div>
</div>
LOL;
// Free result set
mysqli_free_result($result);
}
}
//close mysqli connection
mysqli_close($con);
?>
关于php - 如何在php中制作循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086103/