我的if语句阻止提取数据

时间:2018-02-01 10:41:51

标签: php

我正在开发一个小项目,我的代码包含" if"声明。但是,我注意到每当我获取值为0的数据时,它确实将其恢复(var_dump())但它没有显示它,因为我猜代码认为它是无用的值?

  <?php 

            include_once 'dbh.inc.php';
                $sqli = "SELECT datum, v, vo, nav FROM  months;";
                $result = mysqli_query($conn, $sqli);
                $resultCheck = mysqli_num_rows($result);
                echo "<table>";
                        echo "<tr>";
                            echo "<th>Datum</th>";
                            echo "<th> Vakantie uren</th>";
                            echo "<th> Vakantie uren opgennomen </th>";
                            echo "<th> Nieuw aantal vakantie uren</th>";
                        echo "</tr>";
                    if ($resultCheck > 0) {
                        while ($row = mysqli_fetch_assoc($result)) {
                            if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) {
                echo "<tr>";
                echo "<td>".$row['datum']."</td>";
                echo "<td>".$row['v']."</td>";
                echo "<td>".$row['vo']."</td>";
                echo "<td>".$row['nav']."</td>";
                echo "</tr>";
            }
        }
                echo "</table>";
}   
        ?>

请注意if ($row['datum']

当我拿出这段代码时,它甚至会渲染0的值,但是如果我最终得到的值已经没有填写(例如,你的数据库中有5个表,用户填写4个因为第五个对他来说是无用的)它也取回第五个,我最终得到一个非常丑陋的空位。它看起来不太好。

我的问题是,是否有可能告诉php(即使使用&#34; if&#34;语句)0 的值是我想要显示的值?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你不希望在没有值的情况下显示行,但是当有值时显示它,即使值为零也是如此。

您的if语句不起作用,因为0 = false中的php

因此,请考虑修改if语句,如下所示:

<?php 

        include_once 'dbh.inc.php';
            $sqli = "SELECT datum, v, vo, nav FROM  months;";
            $result = mysqli_query($conn, $sqli);
            $resultCheck = mysqli_num_rows($result);
            echo "<table>";
                    echo "<tr>";
                        echo "<th>Datum</th>";
                        echo "<th> Vakantie uren</th>";
                        echo "<th> Vakantie uren opgennomen </th>";
                        echo "<th> Nieuw aantal vakantie uren</th>";
                    echo "</tr>";
                if ($resultCheck > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        if( (!empty($row['datum']) || $row['datum'] == 0)
                           && (!empty($row['v']) || $row['v'] == 0)
                            && (!empty($row['vo']) || $row['vo'] == 0)
                           && (!empty($row['nav']) || $row['nav'] ==0)
                         ) {
                             echo "<tr>";
                             echo "<td>".$row['datum']."</td>";
                             echo "<td>".$row['v']."</td>";
                             echo "<td>".$row['vo']."</td>";
                             echo "<td>".$row['nav']."</td>";
                             echo "</tr>";
                          }
                  }
            echo "</table>";
}   
    ?>

答案 1 :(得分:0)

好吧,在PHP empty is evaluated as false中,您的if条件将为false

如果要显示值为空的0,可以使用Ternay Operator

$foo = '';
echo $foo ? $foo : '0'; // 0

检查出来:https://3v4l.org/Beem1