我有一个mysqli查询,可从表中获取所有图像(我有5个图像显示)。我正在使用jquery滑块显示它们。问题是如果没有5张图像,我会看到空白页,例如,如果用户仅上传两张图像,则其余三个缩略图将是空白的,当您单击它们时,它将显示空白区域。我不希望发生这种情况,那么如何仅在图像存在时显示缩略图而不显示空白缩略图?

我尝试了以下方法,但这不起作用。我只需要查看image_one是否存在,然后显示缩略图,其余图像也一样。

<?php
 $stmt = $mydb->prepare("SELECT * FROM table where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
 $result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$path = 'images/';
?>


<div id="slides">

<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_one']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_one']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_two']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_two']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?> <div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_three']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_three']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_four']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_four']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_five']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_five']?>"/></a></div><?php };?>

</div>

<div id="slide_menu">

<ul id="slide"> <!-- This is the thumbnail area -->
    <li class="fbar">&nbsp;</li>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_one']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_two']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_three']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_four']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_five']?>"  /></a></li><?php }; ?>
</ul>


</div>

最佳答案

我们看不到您的数据库设计,因此请查看您的默认值

image_one

行计数始终为01(由于Limit 1
重要的是字段image_oneimage_five
这些字段始终存在,无论它们为空还是使用文件名填充。

根据默认值测试一下

例如以下之一


如果($ row ['image_one']>''){
如果($ row ['image_one']> null {


放一个if arround构建html。

<?php if($result->num_rows > 0){?>
 <?php if ($row['image_one'] > '')
   {?>
     <li class="menuItem">
     <a href=""><img src="<?php echo $path.$row['image_one']?>" /></a>
     </li>
   <?php
   }?>
    .... next 4 other tests
 <?php if ($row['image_two'] > '')
    ....

<?php } // END__$result->num_rows > 0 ?>

关于php - 检查特定行是否存在mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19328963/

10-10 18:22
查看更多