doucong1268 2013-04-14 20:49
浏览 46
已采纳

查询为每个循环中连接到行的每个图像创建一条记录

I have this query which pulls a list of rides attached to a particular theme park and shows their details and any attached photos (the photos are pulled from a separate table and connected by 'ride_id'). Below is the query

$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);

The problem is when I list the results pulled from this query it creates duplicate rows for a ride, one for each image.

What is want is:

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

but what I currently have is

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 

Below is the for each loop I'm using. What would I need to alter on the query or loop to have this work correctly?

<?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; ?>

I'm still at the early stages of understanding PHP and MySQL so a detailed answer would really help. Thank you.

  • 写回答

1条回答 默认 最新

  • doupu1949 2013-04-15 10:57
    关注

    I have solved this now with help from elsewhere. If it helps anyone else this is the code to make it happen:

    $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>');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?