duanpin2034 2017-05-22 22:06
浏览 65
已采纳

如果数据库条目为null,则不显示div

Have a database with option1, option2, option3, option4, option5 as columns. Options 1-3 have data entered, however, options 4-5 are NULL.

How can I set it so that a div is NOT displayed if option in database is NULL?

PHP CODE:

 $sql = "SELECT option1, option2, option3, option4, option5 FROM options";
        $result = mysqli_query($conn, $sql);
        $null = NULL;

        if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
                if ($result != $null) {
                    echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option1"]. '</div2>' . '</div>';
                }
                else {
                    echo '<div class="null"></div>';
                }
                if ($result != $null) {
                    echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option2"]. '</div2>' . '</div>';
                }
                else {
                    echo '<div class="null"></div>';
                }
                if ($result != $null) {
                    echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option3"]. '</div2>' . '</div>';
                }
                else {
                    echo '<div class="null"></div>';
                }
                if ($result != $null) {
                    echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option4"]. '</div2>' . '</div>';
                }
                else {
                   echo '<div class="null"></div>';
                }
                if ($result != $null) {
                    echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option5"]. '</div2>' . '</div>';
                }
                else {
                    echo '<div class="null"></div>';
                }
                ...

CSS CODE:

.null {
    display: none;
}

At the moment, even for options 4-5, div class="null" is not working and div class="row" is being displayed (height=70px) but is blank. Thanks.

  • 写回答

2条回答 默认 最新

  • dsx58940 2017-05-22 22:14
    关注

    You are comparing the actual query to null, not the individual columns.

    $result = mysqli_query($conn, $sql);
    

    The line above returns a mysqli-result object, not the rows or columns - use your $row variable in the loop, which holds each row, like this

    if ($row['option1'] != null) {
        echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option1"]. '</div2>' . '</div>';
    } else {
        echo '<div class="null"></div>';
    }
    if ($row['option2'] != null) {
        echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option2"]. '</div2>' . '</div>';
    } else {
        echo '<div class="null"></div>';
    }
    

    See that this code compares $row['option1'] with null, instead of $result compared to null.

    You can also just don't do anything if there are no rows, since you don't display the div at all.

    if ($row['option1'] != null) {
        echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option1"]. '</div2>' . '</div>';
    }
    if ($row['option2'] != null) {
        echo '<div class="row">' . '<div1></div1>' . '<div2>' . $row["option2"]. '</div2>' . '</div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测