PHP,仅在无序列表中返回非空值

时间:2011-02-21 15:15:22

标签: php mysql

我有以下sql查询从建立表中提取图像。 主要照片即,establishment_mainphoto_url始终是输入的,永远不能保留为空。但是,其他5个字段可以包含照片或保留为null,或者是某些null和某些字段的组合。

我有一个jcarousel幻灯片图片显示调用无序列表中的图像进行显示。

代码:

$establishment_id = $row_establishment_view['establishment_id'];
$carouselSQL = mysql_query("SELECT establishment_mainphoto_url,
                                 establishment_photo_url1, 
                                 establishment_photo_url2, 
                                 establishment_photo_url3, 
                                 establishment_photo_url4, 
                                 establishment_photo_url5                                   
                                 FROM establishment 
                                 WHERE establishment_id=$establishment_id");
$carouselImg = mysql_fetch_assoc($carouselSQL);  

jquery轮播代码:

<div class="carousel_container"><!--Carousel list of images-->
                    <div class="infiniteCarousel">
                       <div class="wrapper">
                           <ul>
                           <?php if(!empty($carouselImg['establishment_mainphoto_url'])) {?>                                                                                        
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_mainphoto_url'];?>" height="390" width="500"/></li> 
                           <?php } ?>
                           <?php if(!empty($carouselImg['establishment_photo_url1'])) {?>   
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url1'];?>" height="390" width="500"/></li>
                           <?php } ?>
                           <?php if(!empty($carouselImg['establishment_photo_url2'])) {?>   
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url2'];?>" height="390" width="500"/></li> 
                           <?php } ?>  
                           <?php if(!empty($carouselImg['establishment_photo_url3'])) {?>     
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url3'];?>" height="390" width="500"/></li> 
                           <?php } ?>
                           <?php if(!empty($carouselImg['establishment_photo_url4'])) {?>   
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url4'];?>" height="390" width="500"/></li> 
                           <?php } ?> 
                           <?php if(!empty($carouselImg['establishment_photo_url5'])) {?>      
                               <li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url5'];?>" height="390" width="500"/></li>
                           <?php } ?>                    
                           </ul> 
                       </div>   
                    </div>   
                </div> 

假设用户输入了establishment_mainphoto_url,establishment_photo_url1和establishment_photo_ur4,并将其他字段留空。

现在,旋转木马显示图像,但两者之间有空白。例如,在主图像和图像1之后,它将有2个空白的“图像”,其中2和3应该是,然后图像4将显示,然后是5的空白,然后旋转木马重新开始。

如何修复它以便3张图片在幻灯片中显示为没有“空白”?

2 个答案:

答案 0 :(得分:1)

<div class="carousel_container"><!--Carousel list of images-->
<div class="infiniteCarousel">
   <div class="wrapper">
       <ul>
       <?php
       foreach ( $carouselImg as $image ) {
           if ( empty($image) ) continue;
           echo '<li><img alt="" src="Establishment_Images/' . $image . '" height="390" width="500"/></li>';
       }
       ?>
       </ul> 
   </div>   
</div>   
</div> 

答案 1 :(得分:0)

你确定$carouselImg['establishment_photo_url5']真的是空的吗?生成的html是什么样的?是否应该出现比图像更多的<li>?也许数据库中有一些空格使empty($blah)认为它是非空的?