如果数据库中没有数据,则隐藏<img/>字段

时间:2016-05-14 13:07:16

标签: php jquery html mysql css

所以我从用户输入的数据库中提取数据。如果他们没有提交图像,我想避免使用空白的img框。

例如,如果用户的数据库中没有$ image2,请避免显示该特定图像的类。这甚至可能吗?

任何帮助将不胜感激!

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" >
<?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?>
</a>

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" >
<?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?>
</a>

6 个答案:

答案 0 :(得分:0)

在打印之前测试你的var,而不是:

if(isset($userRow['image2']) && !empty($userRow['image2'])){
 // your code
}

来源:

isset()http://php.net/manual/en/function.isset.php

empty()http://php.net/manual/en/function.empty.php

答案 1 :(得分:0)

在显示img标签之前添加if条件以检查是否isset()图像 - 表示是否设置了image2且image2不为空:

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" >
    <?php if(isset($userRow['image2']) && $userRow['image2'] != "")  { echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>"; } ?>
</a>

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" >
    <?php if(isset($userRow['image3']) && $userRow['image3'] != "") { echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>"; } ?>
</a>

答案 2 :(得分:0)

您可以使用isset()empty()函数检查变量是否已设置且不为空:

isset()

  

确定变量是否已设置且不是NULL

empty()

  

确定变量是否

<?php if(isset($userRow['image2']) && !empty($userRow['image2'])){ { ?>
    <a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" >

<?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?></a>
<?php } ?>

<?php if(isset($userRow['image3']) && !empty($userRow['image3'])){ { ?>
    <a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" >

<?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?></a>
<?php } ?>

答案 3 :(得分:0)

<?php if(isset($userRow['image2']) && !empty($userRow['image2'])){ ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?></a> <?php } if(isset($userRow['image3']) && !empty($userRow['image3'])) { ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?></a><?php } ?>

试试这个

答案 4 :(得分:0)

使用简单的if {}条件,如下所示:

<?php if(!empty($userRow['image2'])) { ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?></a>   
<?php } ?>

<?php if(!empty($userRow['image3'])) { ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?></a>
<?php } ?>

答案 5 :(得分:0)

如果您正在放置图像源代码,那么如果数据库中有图像,它将反映在页面上......