2015-04-12 87 views
-2

我有一個選擇框,在SQL中有一個空行。什麼最好的方式不顯示這個?如何從PHP中的選擇框中刪除空白?

這是我有的細分市場的代碼。

我試過array_filter沒有運氣!

<label for="SS">Seat Style: </label> 
    <select id="SS"> 
    <option>----</option> 
    <? 
    foreach ($liftsecond as $lift){ 
echo '<option>'.$lift["Seat Style"].'</option>'; 

} 
?> 
</select><br> 

回答

2

它應該相當容易。只要做一個if語句檢查空:

<label for="SS">Seat Style: </label> 
<select id="SS"> 
    <option>----</option> 
    <?php 
     // If the value runs, but the value is NULL, 
     // try using array_filter() to remove the null 
     $liftsecond = array_filter($liftsecond); 
     foreach ($liftsecond as $lift) { 
     // If null is not removed, try using this if statement 
     // (you need to know if the null is "NULL" or "null") 
     // This checks both empty and null 
     // if(!empty($lift["Seat Style"]) && $lift["Seat Style"] != 'null') 
     if(!empty($lift["Seat Style"])) { 
    ?> 
    <option><?php echo $lift["Seat Style"]; ?></option> 
    <?php 
     } 
     } 
    ?> 
</select><br> 
+1

更容易選擇擺在首位'其中xx =「」' – 2015-04-12 21:29:03

+0

@Dragon非空行:這兩種解決方案是正確的 – Zl3n

+1

@zlen:我想你的意思袞。這裏有評論btw完整的名稱 - 一個非常酷的隱藏功能。只需輸入一個或兩個字母「@」,然後按下Tab鍵。 – halfer