我在 PHP 中使用 if 语句,如果查询为空,它不会运行 PHP 有效地隐藏网页的该部分。这是我正在使用的 if 语句
if(!empty($result4)){
}
当用于从整个结果中隐藏单个空行时,它在其他地方工作得很好,但是在为整个查询运行时它不起作用。
这是整个 php 代码 if(!empty($result4)){ 如果查询为空,则不应发生,但确实如此。
<?php
if(!empty($result4)){
printf('<h2>%s News' . PHP_EOL, $row['name']);
$sLastStory = '';
foreach ($result4 AS $row4)
{
$sStory = $row4['headline'] . $row4['story'];
if (strcasecmp($sStory, $sLastStory) != 0)
{
if (!empty($sLastStory))
{
}
$sLastStory = $sStory;
printf('<h3>%s</h3>' . PHP_EOL, $row4['headline']);
printf('<h4>%s</h4>' . PHP_EOL, $row4['Displaydate']);
printf('<p>%s</p>' . PHP_EOL, $row4['story']);
}
if(!empty($row4['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: 230px; max-width: 230px" alt="%s"/></a>' . PHP_EOL, $row4
['url'], $row4['alt'], $row4['headline'], $row4['description'],$row4['credit'], $row4['url'], $row4['alt'],
$row4['alt'] );
}
}
printf('
<br>
<hr>
<a class="bloglink" href="parknews.php?park_id=%s">See all %s news</a></li>' . PHP_EOL, $park_id, $row
['name']);
}
?>
任何想法如何使它工作?
如果有帮助,这是 MySQL 查询:
$park_id = $_GET['park_id'];
$query4= 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate, url, alt, description, credit
FROM tpf_news
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id
Where tpf_news.park_id = ' . $park_id .' ORDER BY date DESC';
$result4 = $pdo->query($query4);
谢谢