所以我从用户输入的数据库中提取数据。如果他们没有提交图像,我想避免使用空白的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>
答案 0 :(得分:0)
在打印之前测试你的var,而不是:
if(isset($userRow['image2']) && !empty($userRow['image2'])){
// your code
}
来源:
答案 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)
如果您正在放置图像源代码,那么如果数据库中有图像,它将反映在页面上......