0

I'm trying to build an if statement for when a variable is filled with a url it will display a button and link to the site, and when the variable is empty the link and button will not display. So far I got it to where it is displays the button and links but making it into an if statement keeps on breaking my site. If you see a problem with the code below, please help. Thanks

<div id="social_icon">
<?php if (isset($fburl))
{ 
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"    
height="30"></a>; 
}
else
{
//dont show anything
}
?>   
</div>
0

2 Answers 2

2

You're trying to use HTML within your PHP code, so PHP sees this as an unexpected variable/string. Either use echo for this, or close the PHP statement, and then write your HTML.

Either:

<div id="social_icon">
    <?php if(isset($fburl)){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>

Or:

<div id="social_icon">
    <?php if (isset($fburl)){ 
        echo '<a href="'.$options['fburl'].'"><img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" /></a>'; 
    }else{
        //dont show anything
    } ?>   
</div>

Edit

Actually, I would assume it's not outputting anything because your if statement is checking for $fburl whereas you're echoing the link as $options['fburl']. If the facebook url is located at $options['fburl'], try:

<div id="social_icon">
    <?php if(isset($options['fburl'])){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>

Edit 2

If the options are set but don't contain a link, you will also need check for that:

<div id="social_icon">
    <?php if(isset($options['fburl']) && !empty($options['fburl'])){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

it still isn't being called up. I am using Wordpress so the variables are being called in from another .php page. Do you think that might be why it is affecting it to not display?
What is the HTML output on the page?
its just blank. Like it shows the div around it and just blank space between them
sweet. there up now with the thinks and the output are working good. The problem is that when it is not filled it still outputs the a herf to the flicker link.
I've added another edit. If the options for those other links are defined but empty, that if statement would still pass. Checking for a non-empty value would fix that in that case.
|
0

Syntax error, change it to:

<?php if (isset($fburl))
{
//missed end tag here
?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"    
height="30"></a>; 
<?php
//add another php start tag
}
else
{
//dont show anything
}
?> 

2 Comments

That stopped the breaking but now it doesn't display the link at all, even if there is one set.
It's because you have an error in your syntax. There's an extra " after the src. I've removed it in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.