如果value为空/ null,则不显示列/行

时间:2017-10-17 16:25:57

标签: php mysql

我在一个网页上显示SQL数据,该网页用于显示不同列数的不同表格。对于具有空值的表,我不希望这些表显示在HTML表上。如何创建它以便它们不显示?

    String userAgent = Util.getUserAgent(this, "Bakingapp");
    MediaSource mediaSource = new ExtractorMediaSource(uri, defaultDataSourceFactory,new
            DefaultExtractorsFactory(), null, null);
    mExoPlayer.prepare(mediaSource);
    mExoPlayer.setPlayWhenReady(true);

我也试过了:

<?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();
?>

和is_null,以及:

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

这些变化都没有奏效。它要么显示空表,要么根本不显示任何内容。

enter image description here 它显示空列。如果这些列为空,我不希望这些列出现。

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

数据库数据

1 个答案:

答案 0 :(得分:0)

根据此StackOverflow帖子(PHP: using empty() for empty string?),您不应使用empty来确定字符串长度,因为变量本身就存在。鉴于出现一些复杂的情况,empty最适合用于数组。

根据empty的{​​{3}},以下情况会评估为empty返回TRUE。

以下内容被认为是空的:

  • “”(空字符串)
  • 0(0为整数)
  • 0.0(0作为浮动)
  • “0”(0作为字符串)
  • NULL
  • FALSE
  • array()(空数组)
  • $变种; (声明的变量,但没有值)

因此,我会比较一个空字符串:

if ($row['col1_name'] == ''){echo "<th>".$col1_name."</th>";}
相关问题