user3313407
user3313407

Reputation: 5

Avoid showing icons that don't contain a value

I've already been searching for a couple of hours for a solution. What I want to do is make icons that don't got a value (in the php) not show up.

Here's an image of what I currently have.

So for instance if only twitter and facebook got values, they should only appear on the screen.

Below is the code, and I hope someone got a solution for this. Cheers!

 <ul class="social-buttons">
            <div class="wrapping">

                <a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>

                <a class="social" href="http://facebook.com/<?php echo $profile_data['facebook']; ?>" target="_blank"><li class="facebook"></li></a>

                <a class="social" href="skype:<?php echo $profile_data['skype']; ?>?chat"><li class="skype"></li></a>

                <a class="social" href="http://instagram.com/<?php echo $profile_data['instagram']; ?>"><li class="instagram"></li></a>

                <a class="social" href="http://dribbble.com/<?php echo $profile_data['dribbble']; ?>"><li class="dribbble"></li></a>

                <a class="social" href="http://linkedin.com/in/<?php echo $profile_data['linkedin']; ?>"><li class="linkedin"></li></a>
            </div>
        </ul>

Upvotes: 1

Views: 53

Answers (2)

bettersayhello
bettersayhello

Reputation: 2597

i think you can do like:

<ul class="social-buttons">
    <div class="wrapping">
       <?php if $profile_data['twitter'] {?>
          <a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
       <?php } ?>
         ....

</ul>

Imo, a probably better way to do this is to perform a pre-processing of the data i.e. $profile_data, before you use it in your view so the view would no longer need to handle the processing logic. After which, the view can output your links by using a more concise construct e.g. for loop, that does not use any conditional branching.

Upvotes: 2

Carl0s1z
Carl0s1z

Reputation: 4723

You need to use the if statement with !empty(). The !empty() checks if a variable is NOT empty. Than proceed with the code. Like the example given here:

<?php 
    if(!empty($profile_data['twitter'])){ 
?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php 
    } 
?>

If the variable is empty, it wont give the outputed code, in your case the <a> with the icon.

Upvotes: 3

Related Questions