1

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

3 回答 3

1

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

image_one等等

The row count is allways 0 or 1 (because of Limit 1)
Important are the fields image_one to image_five These fields are always present, regardless of whether they are empty or with file names are filled.

depending on the default value test it

for example one of

  • if ($row['image_one'] > '') {
  • if ($row['image_one'] > null {

put an if arround building 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 ?>
于 2013-10-12T02:05:42.107 回答
0

try something like

   ...
    <ul id="slide"> <!-- This is the thumbnail area -->
        <li class="fbar">&nbsp;</li>
        <?php
        foreach ( $row as $element => $val){
          if(isset($val)) {
            print "<li class='menuItem'><a href=''><img src=".$path.$val."/></a></li>";
          }
        }
        ?>
    </ul>
...

and the same for the 1st (<div id="slides">) block

于 2013-10-12T02:34:59.200 回答
-1

我不是 100% 确定 pdo,但它不会太远

<?php
 $sql = "SELECT * FROM table where title = $title AND id = $id limit 1 ";
 $result = mysql_query($sql);
 while ($row = mysql_fetch_array($result)) {
?>

<div class="slide">
   <a class="fancybox" href="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>" data-fancybox-group="gallery">
      <img class="cloudzoom appsld" src="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>"/>
   </a>
</div>

<?php } // end while loop ?>
</div> <!-- end id="slides" -->
于 2013-10-12T00:12:11.823 回答