0

I'm displaying SQL data on a webpage that is used on different tables that display a different number of columns. For the tables that have null values, I do not want those to show on the HTML table. How do I create it so that they do not display?

<?php
    $sql1 = "SELECT * FROM lists WHERE id = $listid";

    $result1 = $mysqli->query($sql1);
    while ($row = $result1->fetch_assoc()) {
        $id = $row['id'];
        $name = $row['name'];
        $list_id = $row['list_id'];
        $description = $row['description'];
        $status = $row['status'];
        $col1_name = $row['col1_name'];
        $col2_name = $row['col2_name'];
        $col3_name = $row['col3_name'];
        $col4_name = $row['col4_name'];
        $col5_name = $row['col5_name'];
        $col6_name = $row['col6_name'];
        $col7_name = $row['col7_name'];
        $col8_name = $row['col8_name'];
        $col9_name = $row['col9_name'];
        $col10_name = $row['col10_name'];
        $col11_name = $row['col11_name'];
        $col12_name = $row['col12_name'];
        $col13_name = $row['col13_name'];
        $col14_name = $row['col14_name'];
        $col15_name = $row['col15_name'];
     } 

    // $id = $_GET["id"];
    $sql = "SELECT * FROM list_rows WHERE list_id = $listid";

    $result = $mysqli->query($sql);

if ($result->num_rows) {

    echo "<table class='w3-table-all' id='datatable'>
    <thead>
        <tr class='w3-indigo'>";
            if (!empty($row['col1_name'])){echo "<th>".$col1_name."</th>";}
            if (!empty($row['col2_name'])){echo "<th>".$col2_name."</th>";}
            if (!empty($row['col3_name'])){echo "<th>".$col3_name."</th>";}
            if (!empty($row['col4_name'])){echo "<th>".$col4_name."</th>";}
            if (!empty($row['col5_name'])){echo "<th>".$col5_name."</th>";}
            if (!empty($row['col6_name'])){echo "<th>".$col6_name."</th>";}
            if (!empty($row['col7_name'])){echo "<th>".$col7_name."</th>";}
            if (!empty($row['col8_name'])){echo "<th>".$col8_name."</th>";}
            if (!empty($row['col9_name'])){echo "<th>".$col9_name."</th>";}
            if (!empty($row['col10_name'])){echo "<th>".$col10_name."</th>";}
            if (!empty($row['col11_name'])){echo "<th>".$col11_name."</th>";}
            if (!empty($row['col12_name'])){echo "<th>".$col12_name."</th>";}
            if (!empty($row['col13_name'])){echo "<th>".$col13_name."</th>";}
            if (!empty($row['col14_name'])){echo "<th>".$col14_name."</th>";}
            if (!empty($row['col15_name'])){echo "<th>".$col15_name."</th>";}


        echo "</tr>
    </thead>";

    // output data of each row
    echo "<tbody>";

    while($row = $result->fetch_assoc()) {      
        echo "
        <tr class='w3-hover-pale-blue'>";
            if (!empty($row["col1_value"])){echo "<th>".$col1_value."</th>";}
            if (!empty($row["col2_value"])){echo "<th>".$col2_value."</th>";}
            if (!empty($row["col3_value"])){echo "<th>".$col3_value."</th>";}
            if (!empty($row["col4_value"])){echo "<th>".$col4_value."</th>";}
            if (!empty($row["col5_value"])){echo "<th>".$col5_value."</th>";}
            if (!empty($row["col6_value"])){echo "<th>".$col6_value."</th>";}
            if (!empty($row["col7_value"])){echo "<th>".$col7_value."</th>";}
            if (!empty($row["col8_value"])){echo "<th>".$col8_value."</th>";}
            if (!empty($row["col9_value"])){echo "<th>".$col9_value."</th>";}
            if (!empty($row["col10_value"])){echo "<th>".$col10_value."</th>";}
            if (!empty($row["col11_value"])){echo "<th>".$col11_value."</th>";}
            if (!empty($row["col12_value"])){echo "<th>".$col12_value."</th>";}
            if (!empty($row["col13_value"])){echo "<th>".$col13_value."</th>";}
            if (!empty($row["col14_value"])){echo "<th>".$col14_value."</th>";}
            if (!empty($row["col15_value"])){echo "<th>".$col15_value."</th>";}
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
} else {
    echo "0 results";
}
echo "</div>";

//$mysqli->close();
?>

I've also tried:

if (!is_null($row['col1_name'])){echo "<th>".$col1_name."</th>";}

and is_null, as well as:

if ($row['col1_name'] != null){echo "<th>".$col1_name."</th>";}

And none of these variations have worked. It either displays an empty table or displays nothing at all.

enter image description here It displays empty columns. I do not want those columns to appear if they are empty.

col1_name | col2_name | col3_name | col4_name | col5_name | col6_name | col7_name | col8_name | col9_name | col10_name | col11_name | col12_name | col13_name | col14_name | col15_name |
Item------| One_Day---| Three_Days | Ten_Days | Totals----| Notes-----| Facility--| Department| NULL------| NULL-------| NULL-------| NULL-------| NULL-------| NULL-------| NULL-------|

database data

cweave
  • 306
  • 4
  • 13
  • Can you demonstrate the desired result on some sample data? – PM 77-1 Oct 17 '17 at 16:28
  • Also, why are you comparing to the `$row["col15_value"]` database result but printing the newly defined variable `$col15_value`? Shouldn't really matter, but just curious if those variables were used other places. – kchason Oct 17 '17 at 16:42
  • @PM77-1 I amended my original post. – cweave Oct 17 '17 at 16:43
  • 1
    `NULL` values != "empty", you need to remember that. If you want to check either/or, then you need to use the `OR` logical operator. – Funk Forty Niner Oct 17 '17 at 16:44
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Oct 17 '17 at 16:48
  • try using this: $retVal = (condition) ? a : b ; – Masaba James Moses Apr 24 '21 at 07:46

1 Answers1

0

According to this StackOverflow post (PHP: using empty() for empty string?), you shouldn't use empty to determine string length since the variable exists itself. empty is best used for arrays given some of the complicated cases that occur.

According to the PHP documentation for empty, the following cases would evaluate to empty returning TRUE.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Therefore, I would compare against an empty string:

if ($row['col1_name'] == ''){echo "<th>".$col1_name."</th>";}
kchason
  • 2,836
  • 19
  • 25
  • that embedded link you have https://stackoverflow.com/questions/40784959/php-using-empty-for-empty-string I feel is a duplicate for this question. – Funk Forty Niner Oct 17 '17 at 16:46
  • I would agree, assuming that the correction of the use of `empty` solved his/her issue. – kchason Oct 17 '17 at 16:51
  • @KeithChason Using `if ($row['col1_name'] == ''){echo "".$col1_name."";}` didn't change anything. It still displayed the empty column headers. – cweave Oct 17 '17 at 16:56
  • @xxdash, were all of the related (below) `` cells blank too? – kchason Oct 17 '17 at 17:01
  • @KeithChason that is correct. There is no `` data where the `` is empty. – cweave Oct 17 '17 at 17:04