我有这个查询,其中列出了附加到特定主题公园的游乐设施列表,并显示了他们的详细信息和任何附加的照片(照片是从单独的表中拉出来并通过'ride_id'连接)。以下是查询
$park_id = $_GET['park_id'];
$query2="SELECT * FROM `tpf_rides` LEFT JOIN tpf_images
ON tpf_rides.ride_id=tpf_images.ride_id
WHERE tpf_rides.park_id = $park_id AND `type` LIKE '%Roller Coaster%' ORDER BY `name` ASC";
$result2 = $pdo->query($query2);
问题是,当我列出从此查询中提取的结果时,它会为骑行创建重复的行,每个图像一个。
想要的是:
Ride 1 - Type <br>
Manufactured by xxx, Opened xxxx <br>
image1 image2
Ride 2 - Type <br>
Manufactured by xxx, Opened xxxx <br>
image1 image2 image3
但我目前拥有的是
Ride 1 - Type <br>
Manufactured by xxx, Opened xxxx <br>
image1
Ride 1 - Type <br>
Manufactured by xxx, Opened xxxx <br>
image2
Ride 2 - Type <br>
Manufactured by xxx, Opened xxxx <br>
image1
以下是我正在使用的每个循环。我需要在查询或循环上更改哪些内容才能正常工作?
<?php foreach ($result2 as $row2): ?>
<h2 style="display:inline;"><?php echo $row2['name']; ?></h2><h3 style="display:inline;"> - <?php echo $row2['type']; ?></h3>
<h3>Manufactured by <?php echo $row2['make']; ?>, Opened <?php echo $row2['opened']; ?> </h3>
<img border="0" src="<?php echo $row2['url']; ?>" style="max-height:250px; max-width:250px;" >
<br>
<?php endforeach; ?>
我仍处于理解PHP和MySQL的早期阶段,因此详细的答案确实会有所帮助。谢谢。
答案 0 :(得分:0)
我现在在其他地方的帮助下解决了这个问题。如果它可以帮助其他任何人这就是实现它的代码:
$sLastRide = '';
foreach ($result2 AS $row)
{
$sRide = $row['name'] . $row['type'];
if (strcasecmp($sRide, $sLastRide) != 0)
{
if (!empty($sLastRide))
{
print('</div>' . PHP_EOL);
}
$sLastRide = $sRide;
print('<div>' . PHP_EOL);
printf('<h2 style="display:inline;">%s</h2><h3 style="display:inline;"> - %s</h3>' . PHP_EOL, $row['name'], $row['type']);
printf('<h3>Manufactured by %s, Opened %s</h3>' . PHP_EOL, $row['make'], $row['opened']);
}
printf('<img src="%s" style="max-height: 250px; max-width: 250px" alt=""/>' . PHP_EOL, $row['url']);
}
print('</div>');