doutinghou6980 2018-07-10 01:13
浏览 8
已采纳

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

In the design that I am implemented, is to have an order of each news by their categories.

When there are news related to that category, the results are displayed correctly without any problem

Example:

 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

My little problem in the code of the query, is that when there is no news for a category, the title of that category is displayed, resulting in the following:

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

CUP AMERICA

So my question is, how can I avoid showing the category title when there is no news related to that category?

In order to have, a consequent result:

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

CUP AMERICA

My code

$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";
}

Additional Information

enter image description here

  • 写回答

1条回答 默认 最新

  • dtm37893 2018-07-10 01:25
    关注

    There is little change in your code. First, query the news according to the category. Second, check if there is the news or not. If not, don't show the category.

    $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()) {    
            $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) {
                echo '<div class="date-text">
                    <h4>'.$title_category.'</h4>
                    <ul>';
                $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 "There is no news";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测