exclude empty image fields mysql / php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sensas
    New Member
    • Jun 2010
    • 2

    exclude empty image fields mysql / php

    Sorry for this noob Huh question, but I am calling images from a database and I don't want to show the empty images.

    Example webpage here

    As you can see, there are 7 empty image fields left. How do I exclude empty fields in the code below

    Code:
    Code:
                 <a href="<?php echo $listing_photo1; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo1; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo2; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo2; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo3; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo3; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo4; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo4; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo5; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo5; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo6; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo6; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo7; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo7; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo8; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo8; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo9; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo9; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo10; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo10; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo11; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo11; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo12; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo12; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo13; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo13; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo14; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo14; ?>" width="90" height="66" /></a>
                 <a href="<?php echo $listing_photo15; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo15; ?>" width="90" height="66" /></a>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the simple answer: don’t print 'em.

    essentially, this is easier to accomplish with a function (among others):

    Code:
    function printLink($url = '')
    {
        if (!empty($url))
        {
            $link = '<a href="%s" rel="lightbox[roadtrip]"><img src="%s" width="90" height="66"></a>';
            printf($link, $url, $url);
        }
    }
    
    // ex:
    printLink($listing_photo1);
    // etc.

    Comment

    • Sensas
      New Member
      • Jun 2010
      • 2

      #3
      Perfect, Thanks for the quick answer!

      At this moment I solved it by:

      Code:
      <?php if(isset($listing_photo1) && $listing_photo1 != "") { ?><a href="<?php echo $listing_photo1; ?>" rel="lightbox[roadtrip]"><img src="<?php echo $listing_photo1; ?>" width="90" height="66" /></a><?php  } ?>
      Works also.

      Now the next thing is to make the uploader easier and ad an arrey ;-)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        sure, but you have to repeat it for every variable.

        Comment

        Working...