我使用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> $dining->title</h3>
<h4> $dining->Comforts</h4>
$dining->com1<br/>
$dining->com2<br/>
$dining->com3<br/>
$dining->com4<br/>
$dining->com5<br/>
$dining->com6<br/>
<h4> $dining->Breakers</h4>
$dining->break1<br/>
$dining->break2<br/>
<h4> $dining->Wild</h4>
$dining->wild1<br/>
$dining->wild2<br/>
<h4> $dining->Tureens</h4>
$dining->tur1<br/>
$dining->tur2<br/>
$dining->tur3<br/>
<h4> $dining->Desserts</h4>
$dining->des1<br/>
$dining->des2<br/>
$dining->des3<br/>
$dining->des4<br/><br/>
";
}
?>
答案 0 :(得分:1)
如果要抑制空白值,则必须单独测试每个值以决定是否应输出,例如
if (strlen($dining->des1) > 0) {
echo " {$dining->des1}<br />";
}
答案 1 :(得分:0)
if(isset(trim($dining->wild1)){
echo " {$dining->wild1}<br />";
}
答案 2 :(得分:0)
这可能符合法案:
<?php
while($dining->results())
{
echo "<span class='date'>". date("F jS, Y", strtotime($dining->date)) ."</span>";
if( !empty( $dining->title ) ) echo "<h3> $dining->title</h3>";
if( !empty( $dining->Comforts ) ) echo "<h4> $dining->Comforts</h4> ";
if( !empty( $dining->com1 ) ) echo " $dining->com1<br/>";
if( !empty( $dining->com2 ) ) echo " $dining->com2<br/>";
if( !empty( $dining->com3 ) ) echo " $dining->com3<br/>";
if( !empty( $dining->com4 ) ) echo " $dining->com4<br/>";
if( !empty( $dining->com5 ) ) echo " $dining->com5<br/>";
if( !empty( $dining->com6 ) ) echo " $dining->com6<br/>";
if( !empty( $dining->Breakers ) ) echo "<h4> $dining->Breakers</h4>";
if( !empty( $dining->break1 ) ) echo " $dining->break1<br/>";
if( !empty( $dining->break2 ) ) echo " $dining->break2<br/>";
if( !empty( $dining->Wild ) ) echo "<h4> $dining->Wild</h4>";
if( !empty( $dining->wild1 ) ) echo " $dining->wild1<br/>";
if( !empty( $dining->wild2 ) ) echo " $dining->wild2<br/>";
if( !empty( $dining->Tureens ) ) echo "<h4> $dining->Tureens</h4>";
if( !empty( $dining->tur1 ) ) echo " $dining->tur1<br/>";
if( !empty( $dining->tur2 ) ) echo " $dining->tur2<br/>";
if( !empty( $dining->tur3 ) ) echo " $dining->tur3<br/>";
if( !empty( $dining->Desserts ) ) echo "<h4> $dining->Desserts</h4>";
if( !empty( $dining->des1 ) ) echo " $dining->des1<br/>";
if( !empty( $dining->des2 ) ) echo " $dining->des2<br/>";
if( !empty( $dining->des3 ) ) echo " $dining->des3<br/>";
if( !empty( $dining->des4 ) ) echo " $dining->des4<br/><br/>";
}
?>
当值为NULL或包含空字符串时,PHP的empty()
将返回true。 More in the manual