2013-09-24 100 views
0

我使用PHP代碼中的SQL查詢來顯示網站上數據庫的字段。有沒有辦法在空(NULL)字段的輸出中刪除空格(空格)?不顯示空格

這裏是我的代碼:

<?php 
    $sql = "SELECT * FROM dining ORDER BY date DESC LIMIT 0, 3"; 

    $dining = new Database($sql); 
    while($dining->results()){ 

    echo "   
        <span class='date'>". date("F jS, Y", strtotime($dining->date)) ."</span> 
<h3>&nbsp;$dining->title</h3> 
<h4>&nbsp;$dining->Comforts</h4> 
&nbsp;$dining->com1<br/> 
&nbsp;$dining->com2<br/> 
&nbsp;$dining->com3<br/> 
&nbsp;$dining->com4<br/> 
&nbsp;$dining->com5<br/> 
&nbsp;$dining->com6<br/> 
<h4>&nbsp;$dining->Breakers</h4> 
&nbsp;$dining->break1<br/> 
&nbsp;$dining->break2<br/> 
<h4>&nbsp;$dining->Wild</h4> 
&nbsp;$dining->wild1<br/> 
&nbsp;$dining->wild2<br/> 
<h4>&nbsp;$dining->Tureens</h4> 
&nbsp;$dining->tur1<br/> 
&nbsp;$dining->tur2<br/> 
&nbsp;$dining->tur3<br/> 
<h4>&nbsp;$dining->Desserts</h4> 
&nbsp;$dining->des1<br/> 
&nbsp;$dining->des2<br/> 
&nbsp;$dining->des3<br/> 
&nbsp;$dining->des4<br/><br/> 
"; 
} 
?> 
+0

空間和空白空間有什麼區別? –

回答

1

如果你想禁止空值,你必須單獨測試每個值來決定是否應該輸出與否,例如

if (strlen($dining->des1) > 0) { 
    echo "&nbsp;{$dining->des1}<br />"; 
} 
+0

這工作知府,現在所有的空白空間都不見了,非常感謝。 –

0
if(isset(trim($dining->wild1)){ 
    echo "&nbsp;{$dining->wild1}<br />"; 
} 
0

這可能符合該法案:

<?php 
while($dining->results()) 
{ 
    echo "<span class='date'>". date("F jS, Y", strtotime($dining->date)) ."</span>"; 

if(!empty($dining->title)) echo "<h3>&nbsp;$dining->title</h3>"; 
if(!empty($dining->Comforts)) echo "<h4>&nbsp;$dining->Comforts</h4> "; 
if(!empty($dining->com1)) echo "&nbsp;$dining->com1<br/>"; 
if(!empty($dining->com2)) echo "&nbsp;$dining->com2<br/>"; 
if(!empty($dining->com3)) echo "&nbsp;$dining->com3<br/>"; 
if(!empty($dining->com4)) echo "&nbsp;$dining->com4<br/>"; 
if(!empty($dining->com5)) echo "&nbsp;$dining->com5<br/>"; 
if(!empty($dining->com6)) echo "&nbsp;$dining->com6<br/>"; 
if(!empty($dining->Breakers)) echo "<h4>&nbsp;$dining->Breakers</h4>"; 
if(!empty($dining->break1)) echo "&nbsp;$dining->break1<br/>"; 
if(!empty($dining->break2)) echo "&nbsp;$dining->break2<br/>"; 
if(!empty($dining->Wild)) echo "<h4>&nbsp;$dining->Wild</h4>"; 
if(!empty($dining->wild1)) echo "&nbsp;$dining->wild1<br/>"; 
if(!empty($dining->wild2)) echo "&nbsp;$dining->wild2<br/>"; 
if(!empty($dining->Tureens)) echo "<h4>&nbsp;$dining->Tureens</h4>"; 
if(!empty($dining->tur1)) echo "&nbsp;$dining->tur1<br/>"; 
if(!empty($dining->tur2)) echo "&nbsp;$dining->tur2<br/>"; 
if(!empty($dining->tur3)) echo "&nbsp;$dining->tur3<br/>"; 
if(!empty($dining->Desserts)) echo "<h4>&nbsp;$dining->Desserts</h4>"; 
if(!empty($dining->des1)) echo "&nbsp;$dining->des1<br/>"; 
if(!empty($dining->des2)) echo "&nbsp;$dining->des2<br/>"; 
if(!empty($dining->des3)) echo "&nbsp;$dining->des3<br/>"; 
if(!empty($dining->des4)) echo "&nbsp;$dining->des4<br/><br/>"; 

} 
?> 

PHP的empty()會當你的值爲NULL或包含一個空字符串返回true。 More in the manual

+1

我試過並沒有工作,謝謝你的工作很多,我很抱歉:/ –

+0

沒關係。我想知道爲什麼它不會工作。 –

+1

不要使用空這樣的東西。它會炸燬你,特別是在'0'是允許值的地方。空(0)爲真。 –