没有与该类别相关的新闻时,如何避免显示该类别的标题?

时间:2018-07-10 01:13:30

标签: php mysqli

在我实现的设计中,是按类别对每个新闻进行排序。

当有与该类别有关的新闻时,结果正确显示而没有任何问题

示例:

 PREMIER LEAGUE
 Lorem ipsum dolor sit amet, consectetur adipiscing elit 1
 Lorem ipsum dolor sit amet, consectetur adipiscing elit 2

 CUP AMERICA
 Lorem ipsum dolor sit amet, consectetur adipiscing elit 3
 Lorem ipsum dolor sit amet, consectetur adipiscing elit 4

我在查询代码中的小问题是,当没有类别的新闻时,将显示该类别的标题,从而导致以下结果:

PREMIER LEAGUE
Lorem ipsum dolor sit amet, consectetur adipiscing elit 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit 2

CUP AMERICA

所以我的问题是,当没有与该类别相关的新闻时,如何避免显示该类别的标题?

为了得到以下结果:

PREMIER LEAGUE
Lorem ipsum dolor sit amet, consectetur adipiscing elit 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit 2

CUP AMERICA

我的代码

$stmtcategory = $con->prepare("SELECT id_category,title_category FROM category WHERE active=?");
$stmtcategory->bind_param("i",$active);
$active = "1";
$stmtcategory->execute();
$stmtcategory->store_result();

if ($stmtcategory->num_rows>0) {
    $stmtcategory->bind_result($id_category, $title_category);
    while ($stmtcategory->fetch()) {
        echo '<div class="date-text">
            <h4>'.$title_category.'</h4>
            <ul>';

        $stmt = $con->prepare("SELECT id_one_more_news,cover_page,title,description,detail,url,date_post FROM one_more_news WHERE id_category=? order by id_one_more_news ASC LIMIT 2");
        $stmt->bind_param("i",$id_category);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows>0) {
            $stmt->bind_result($id_one_more_news, $cover_page, $title, $description, $detail, $url, $date_post);
            while ($stmt->fetch()) {
                echo '<li>'.$title.'</li>';
            }

            echo '</ul>';
            echo '</div>';
        } else {
            //echo "<span>There is no news</span>";
            echo '</ul>';
            echo '</div>';
        }
    }
} else {
    echo "There is no news";
}

其他信息

enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码几乎没有变化。首先,根据类别查询新闻。其次,检查是否有新闻。如果不是,则不显示类别。

/home/RG/clouddrive/.cloudconsole