hotels_data表
hotel_rooms表
MYSQL QUERY
$pdo = $dbo->prepare('SELECT distinct hotels_data.*, hotel_rooms.hid, hotel_rooms.room_name, hotel_rooms.price
FROM hotels_data
LEFT OUTER JOIN hotel_rooms
ON hotels_data.hotel_id = hotel_rooms.hid
');
输出数据
我循环酒店房间
$row = $pdo->fetchAll(PDO::FETCH_OBJ);
foreach($row as $rows){
print '
<!-- Listing -->
<div class="list_hotels_box">
<ul class="list_hotels_ul">
<li style="display:block;">
<img src="../img/hotel_img.png" alt="" style="width: 180px; height:180px;"/>
</li>
<li style="width: 100%;">
<a href="javascript:void()"><h3 class="clear-top">'.$rows->hotel_name.'<span><?php print $star; ?></span></h3></a>
<p>'.$rows->hotel_address.', '.$rows->hotel_state.''.$rows->hotel_country.' </p>
<div id="rooms">
<p>
<table class="table table-condensed table-striped table-hover table-bordered">
';
foreach($row as $td){
if($td->hotel_id == $rows->hid){
print
'
<tr>
<td>'.$td->room_name.'</td>
<td align="right" style="color: darkorange;"><strong><sup>RM</sup>'.$td->price.'</strong></td>
</tr>
';
};
} ;
print '
<tr>
<td colspan="2"><a href="#" class="a" style="font-size: 11px;">See All Rooms</a></td>
</tr>
</table>
</p>
</div>
</li>
<div align="right">
<li>
<h4><small>Rating</small> <strong>8.5</strong></h4>
<span class="help-block clear">"0" Reviews</span>
<button onclick="alert(\'Not operational yet\')" class="btn btn-sm btn-info">Book Now</button>
</li>
</div>
</ul>
</div>
<!-- Listing -->
';
这是输出 我有Hotel Sample&amp;酒店样品2但酒店样品已输出2 我认为它的2间房间的bcoz 所以我的问题是如何删除重复数据
请帮助我。 TQ
抱歉,如果我没有说清楚答案 0 :(得分:0)
一种方法是简单地拆分为2个查询,例如:
SELECT * FROM hotels_data;
然后在您的酒店循环内查询房间:
SELECT hid, room_name, price FROM hotel_rooms WHERE hid = :hotel_id
这假设您使用binded parameters并将$rows->hotel_id
绑定到:hotel_id
参数。
您也可以使用GROUP BY hotel_id
,但除非您使用GROUP_CONCAT()
并解析连锁的房间价值,否则您将无法获得酒店的所有房间。