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

标签 php mysql

我有以下 SQL 查询,用于从建立表中获取图像。 主照片,即,estation_mainphoto_url 始终是输入的,并且永远不能留空。但是,其他 5 个字段可以包含照片或保留为空,或者一些为空和一些不为空的组合。

我有一个 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> 

假设用户输入estination_mainphoto_url、estination_photo_url1 和estination_photo_ur4 并将其他字段留空。

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

如何解决这个问题,使 3 张图像在幻灯片中对齐显示,没有“空白”?

最佳答案

<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> 

关于PHP,仅在无序列表中返回非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5067643/

相关文章:

mysql - 使用 Where 的 SQL 查询错误

mysql - on 子句 MySQL 中的未知列

php - 所有系统的浮点运算都一样吗?

php - 基本 PHP 脚本中 undefined offset 注意事项

php - 如何使用这个 jquery 函数关闭模态窗口?

php - mysql 查询子父关系

mysql - 使用触发器有条件复制 MySQL 表

php - mysql无法执行user_id

php - Mysql查询$row返回 "bool(true)"而不是结果数组

php - Mysqli 查询不返回任何行