我有一个新闻蒸汽从一个MySQL表显示的东西,如标题,故事和日期。这些新闻故事是从PHP循环中生成的,运行良好。
我还有两个MySQL表,一个用于图像,另一个用于视频。这些表有一个名为news_id的列,它与新闻表的唯一字段链接。这个想法是每个新闻故事都会自动显示任何视频或图片。
但是,这个代码不能正确工作,例如,如果一个故事有3个匹配的图像和一个视频,它实际上输出了该视频3次,每幅图片旁边一次。同样,如果有5个视频和一张图片,它将显示该图片5次。
当它只是在新闻旁边拉图片时,代码工作正常,但是由于我也添加了视频表,问题就开始了。我认为我需要重写查询和foreach循环,但不知道如何重写。我的猜测是,我需要两个领头循环在主前端-一个用于图像和一个用于视频。
我希望它能发挥作用:
新闻表格的标题、日期和故事
从图像表中提取的任何照片url
从视频表中提取的任何视频url
然后是下一个标题等等。有人能帮我整理一下这个吗?
作为参考,这是当前使用的查询:
$sql = 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate, name, logo, tpf_parks.park_id, url, alt, description, credit, location
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id
LEFT JOIN tpf_videos ON tpf_news.news_id = tpf_videos.news_id
ORDER BY date DESC' ;
$result = $pdo->query($sql);而当前的php使用:
$sLastStory = '';
foreach ($result AS $row)
{
$sStory = $row['headline'] . $row['story'];
if (strcasecmp($sStory, $sLastStory) != 0)
{
if (!empty($sLastStory))
{
print('<hr>' . PHP_EOL);
}
$sLastStory = $sStory;
printf('<h2>%s</h2>' . PHP_EOL, $row['headline']);
printf('<h3><a href="parknews.php?park_id=%s">
%s</a> - %s</h3>' . PHP_EOL, $row['park_id'], $row['name'], $row['Displaydate']);
printf('<p>%s</p>' . PHP_EOL, $row['story']);
}
if(!empty($row['url'])){
printf('
<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s - Credit - %s" >
<img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row['url'], $row['alt'], $row['headline'], $row['description'],$row['credit'], $row['url'], $row['alt'], $row['alt'] );
}
if(!empty($row['location'])){
printf('<iframe width="640" height="360" src="%s" allowfullscreen></iframe>' . PHP_EOL, $row['location'] );
}
},我怎样才能使这段代码正确地显示我想要的数据呢?,但是正如我所说的,我认为这一切都需要完全重写。
发布于 2013-07-12 05:44:35
你可以用GROUP_CONCAT。这将返回一个连接字符串,然后可以使用explode()。这将使您返回每标题1行,这样您就可以轻松地分组您的图像和视频。
$sql = 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate,
name, logo, tpf_parks.park_id,
GROUP_CONCAT(url) as url, GROUP_CONCAT(alt) as alt, GROUP_CONCAT(description) as description,
GROUP_CONCAT(credit) as credit, GROUP_CONCAT(DISTINCT location) as location
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id
LEFT JOIN tpf_videos ON tpf_news.news_id = tpf_videos.news_id
GROUP BY tpf_images.news_id, tpf_videos.news_id
ORDER BY date DESC' ;
$result = $pdo->query($sql);然后在循环中,在连接的每个列上使用explode() -
if(!empty($row['url'])){
$url=explode(',',$row['url']);
$alt=explode(',',$row['alt']);
$description=explode(',',$row['description']);
$credit=explode(',',$row['credit']);
for($i=0;$i<count($url);$i++){
printf('
<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s - Credit - %s" >
<img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $url[$i], $alt[$i], $row['headline'], $description[$i],$credit[$i], $url[$i], $alt[$i], $alt[$i] );
}
}
if(!empty($row['location'])){
$location=explode(',',$row['location']);
for($j=0;$j<count($location);$j++){
printf('<iframe width="640" height="360" src="%s" allowfullscreen></iframe>' . PHP_EOL, $location[$j] );
}
}https://stackoverflow.com/questions/17606324
复制相似问题