0

我有这个查询,它会拉出附加到特定主题公园的游乐设施列表,并显示它们的详细信息和任何附加的照片(照片是从单独的表中提取的,并通过“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 的早期阶段,因此详细的答案将非常有帮助。谢谢你。

4

1 回答 1

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>');
于 2013-04-15T10:57:21.937 回答