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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?