2017-10-17 198 views
0

我在顯示不同列數的不同表上使用的網頁上顯示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>";} 

,沒有這些變化都工作過。它顯示一個空表或根本不顯示任何內容。

enter image description here 它顯示空列。如果這些列是空的,我不希望這些列出現。

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-------| 

數據庫中的數據

+0

是否可以說明一些樣本數據所期望的結果? –

+0

另外,你爲什麼要比較'$ row [「col15_value」]'數據庫結果,但是打印新定義的變量'$ col15_value'?應該不是很重要,但只是好奇這些變量是否被其他地方使用過。 – kchason

+0

@ PM77-1我修改了我原來的帖子。 – xxdash

回答

0

根據這個StackOverflow的職位(PHP: using empty() for empty string?),你不應該使用empty確定字串長度自變量存在本身。 empty最適合用於發生一些複雜情況的數組。

根據PHP documentation對於empty,以下情況將評估爲返回TRUE的empty

以下的事情被認爲是空的:

  • 「」(空字符串)
  • 0(0作爲整數)
  • 0.0(0爲float)
  • 「 0" (0作爲字符串)
  • NULL
  • FALSE
  • 陣列()(空數組)
  • $ var; (變量聲明,但沒有一個值)

因此,我比較反對一個空字符串:

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

嵌入式鏈接你有https://stackoverflow.com/questions/40784959/php-using-empty-for-empty-string我覺得這是一個重複的問題。 –

+0

我同意,假設使用'empty'的問題解決了他/她的問題。 – kchason

+0

@KeithChason使用'if($ row ['col1_name'] ==''){echo「」。$ col1_name。「」;}'沒有改變任何東西。它仍然顯示空的列標題。 – xxdash