我在顯示不同列數的不同表上使用的網頁上顯示SQL數據。對於具有空值的表,我不希望那些顯示在HTML表上。我如何創建它以便它們不顯示?如果值爲空/空不顯示列/行
<?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();
?>
我也試過:
if (!is_null($row['col1_name'])){echo "<th>".$col1_name."</th>";}
和is_null,以及:
if ($row['col1_name'] != null){echo "<th>".$col1_name."</th>";}
,沒有這些變化都工作過。它顯示一個空表或根本不顯示任何內容。
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-------|
數據庫中的數據
是否可以說明一些樣本數據所期望的結果? –
另外,你爲什麼要比較'$ row [「col15_value」]'數據庫結果,但是打印新定義的變量'$ col15_value'?應該不是很重要,但只是好奇這些變量是否被其他地方使用過。 – kchason
@ PM77-1我修改了我原來的帖子。 – xxdash