隐藏空字段

时间:2011-06-20 20:56:37

标签: php field

我正试图隐藏以下空字段:

<?php 

$events = get_group('Gallery');
foreach($events as $event){  ?>

<a href="<?php echo $event['gallery_image_1'][1]['o']; ?>"><img src="<?php echo     $event['gallery_image_1'][1]['t']; ?>" /></a>
<a href="<?php echo $event['gallery_image_2'][1]['o']; ?>"><img src="<?php echo $event['gallery_image_2'][1]['t']; ?>" /></a>
<a href="<?php echo $event['gallery_image_3'][1]['o']; ?>"><img src="<?php echo $event['gallery_image_3'][1]['t']; ?>" /></a>

<?php }?>

此时所有字段都会显示那里是否有实际图像。我尝试了很多解决方案,但没有运气,所以任何帮助都会非常感激

非常感谢

3 个答案:

答案 0 :(得分:3)

使用array_key_exists

if (array_key_exists('gallery_image_1', $event) { ?>
    <a href="<?php echo $event['gallery_image_1'][1]['o']; ?>"><img src="<?php echo     $event['gallery_image_1'][1]['t']; ?>" /></a>
<?php }

答案 1 :(得分:0)

回显who链接而不仅仅是url和path。这样,如果没有图像,您可以选择不显示链接。

if (!empty($event['gallery_image_1'][1]['t'])) {
    echo "<a href=\"" . $event['gallery_image_1'][1]['o'] . "\"><img src=\"" . $event['gallery_image_1'][1]['t'] . "\" /></a>";
}

答案 2 :(得分:0)

也许这很有帮助,但我不确定你的问题非常广泛:

<?php 

$events = get_group('Gallery');
foreach($events as $event){ 

  for($i=1;$i<4;$i++) {
    if(empty($event['gallery_image_'.$i])) 
      continue
      ;
    $image = $event['gallery_image_'.$i];
?>

<a href="<?php echo $image[1]['o']; ?>"><img src="<?php echo $image[1]['t']; ?>" /></a>

<?php } }?>