0

I am working on a small project, and I have code that contains an "if" statement. However, I noticed whenever I fetch data with the value of 0 it does bring it back (var_dump()) but it doesn't display it because I guess the code thinks it's a useless value?

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

please notice the if ($row['datum'] etc.

When I take this piece of code out it renders even the values of 0, however if I end up having values that haven't been filled out (for example u have 5 tables in your database and the user fills 4 in because the fifth one is a useless one for him) it fetches the 5th one back as well and I end up having a very ugly empty spot. It doesn't look very nice.

My question is, is there a possible way to tell php (even with an "if" statement) that the value 0 is a value I want displayed?

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Why not remove the `if($row['datum']...)` and just display anything the row contains? – Dale Feb 01 '18 at 10:44
  • Your `if` checks if _all_ fetched values are evaluated as `true`, but [0 is considered `false`](http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting) in PHP. – FirstOne Feb 01 '18 at 10:44
  • try `if ($resultCheck >= 0)` – Tynan J Feb 01 '18 at 10:45
  • Because let's say, you fill in 4 rows instead of 5 it will still display all 5.. but the fifth one hasnt been filled out so that will leave a gap.. I mean, is it something that's going to kill me? No, is it something I would like to improve on? Yes. –  Feb 01 '18 at 10:45
  • Please read: https://stackoverflow.com/help/how-to-ask – k0pernikus Feb 01 '18 at 10:45
  • @Tynan already tried that, but the $resultcheck has nothing to do with the value of 0 not being displayed. –  Feb 01 '18 at 10:46
  • Possible duplicate of [Null vs. False vs. 0 in PHP](https://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php) – CBroe Feb 01 '18 at 11:03

2 Answers2

0

If I understand you correctly, You don't want to display the row when there are no values but you want to display it when there are values even if the values are zeros.

Your if statement does not work because 0 = false in php.

Therefore consider modifying your if statement like so:

<?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>";
}   
    ?>
k32y
  • 407
  • 6
  • 11
  • @FirstOne Yes you are right, 0 is considered empty in php. I've modified my answer to also check if value is zero. – k32y Feb 01 '18 at 11:12
  • @k32y and firstone, thank you for ur help. You guys deserve a medal. It finally worked. My grattitude towards you guys is immeasurable. –  Feb 01 '18 at 11:16
0

Well, in PHP, empty is evaluated as false, so your if condition will be false.

If you want to display 0 where the value is empty, you can use the Ternay Operator:

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

Check it out: https://3v4l.org/Beem1.

FirstOne
  • 6,033
  • 7
  • 26
  • 45