2015-04-12 35 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