Ich zeige SQL-Daten auf einer Webseite an, die für verschiedene Tabellen verwendet wird, die eine andere Anzahl von Spalten anzeigen. Für die Tabellen mit Nullwerten möchte ich nicht, dass sie in der HTML-Tabelle angezeigt werden. Wie erstelle ich es so, dass sie nicht angezeigt werden?Spalte/Zeile nicht anzeigen, wenn Wert leer/null ist
<?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();
?>
Ich habe auch versucht:
if (!is_null($row['col1_name'])){echo "<th>".$col1_name."</th>";}
und is_null sowie:
if ($row['col1_name'] != null){echo "<th>".$col1_name."</th>";}
Und keine dieser Varianten gearbeitet haben. Es zeigt entweder eine leere Tabelle an oder zeigt gar nichts an.
Es zeigt leere Spalten an. Ich möchte nicht, dass diese Spalten angezeigt werden, wenn sie leer sind.
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-------|
Datenbank Daten
Können Sie das gewünschte Ergebnis auf einige Beispieldaten zeigen? –
Warum vergleichen Sie auch das Datenbankergebnis '$ row [" col15_value "]', sondern die neu definierte Variable '$ col15_value'? Eigentlich nicht wichtig, aber nur neugierig, ob diese Variablen an anderen Orten verwendet wurden. – kchason
@ PM77-1 Ich habe meine ursprüngliche Post geändert. – xxdash