0

I'm trying to create an if-else statement, that will return different divs. I believe it is failing because there are too many ' in the else statement.

<?php $blogentryid = get_the_ID(); 

if ($blogentryid=="1572") {
echo '<div>Hello</div>'
}

else {
echo '<div class="socialnews2"><!-- start div social-->

                                <div class="twitternews2">
                                <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php the_title(); ?>" d
ata-url="<?php the_permalink() ?>" data-via="giantmangocom">Tweet</a>
                                <script type="text/javascript">
                                //async script, twitter button fashiolista.com style
                                (function() {
                                var s = document.createElement('SCRIPT');
                                var c = document.getElementsByTagName('script')[0];
                                s.type = 'text/javascript';
                                s.async = true;
                                s.src = 'http://platform.twitter.com/widgets.js';
                                c.parentNode.insertBefore(s, c);
                                 })();
                                </script>
                                </div>
                        </div>
                                <div class="facebooknews2">
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=button_count&
amp;show_faces=false&amp;width=80&amp;action=like&amp;colorscheme=light;height=21"" scrolling="no" frameborder="0" style="border:none; overflow:hidden; wid
th:80px; height:21px;" allowTransparency="true"></iframe>
                                </div>'

}

?>

5 Answers 5

6

When you want to show conditional content, open and close the <?php tags instead of using echo statements.

<?php if ($blogentryid == "1572") { ?>

<div>Hello</div>

<?php } else { ?>

<div class="socialnews2"><!-- start div social-->
... rest of content here

<? } ?>

It will also ensure that your php code inside of the div gets evaluated as you intended.

Sign up to request clarification or add additional context in comments.

Comments

0

The ' in your javascript sections need to be escaped: \', otherwise they'll be seen by PHP as the end of the string you're echoing.

For large blobs of text like that, look into HEREDOCs, which allow you to output multi-line strings without any worries about quote escaping.

As well, you can't embed php blocks within strings as you are:

echo 'hello<?php echo "there" ?>'

will not work.

Comments

0

you cannot have a <?php ?> tag inside a <?php ?> tag.

<?php .. data-text="<?php the_title(); ?>" ...?>

maybe do something like this:

<?php echo '... data-text="'; echo the_title(); echo '...'; ?>

Comments

0

This code is all over the place.

At the end of every line of executable code, you must have a semi-colon (";") before the syntax is considered valid. When you're trying to assemble PHP plus HTML, it isn't necessary to echo everything. Here's a cleaned up version of your code below.

As you can see, when you don't need any PHP logic, you can end php with ?> and return to normal HTML. You seemed to have that idea, but you contained it all in an echo statement. That would have worked, but you had some <?php echo $whatever; ?> statements mixed in. Take a look at the cleaned up code I posted and you should be able to see where you were going wrong.

<?php 
    $blogentryid = get_the_ID(); 

    if ($blogentryid=="1572") {
        echo '<div>Hello</div>';
    } else {
?>
<div class="socialnews2"><!-- start div social-->'
    <div class="twitternews2">
        <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php the_title(); ?>" data-url="<?php the_permalink() ?>" data-via="giantmangocom">
            Tweet
        </a>
        <script type="text/javascript">
        //async script, twitter button fashiolista.com style
        (function() {
            var s = document.createElement('SCRIPT');
            var c = document.getElementsByTagName('script')[0];
            s.type = 'text/javascript';
            s.async = true;
            s.src = 'http://platform.twitter.com/widgets.js';
            c.parentNode.insertBefore(s, c);
            })();
        </script>
    </div>
    <div class="facebooknews2">
        <iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=button_count&amp;show_faces=false&amp;width=80&amp;action=like&amp;colorscheme=light;height=21"" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:21px;" allowTransparency="true"></iframe>
    </div>
</div>
<?php
} // end :: if
?>

Comments

0

For multiline strings that are as complex as the one you are suggesting, I would suggest simply ending your php block before the output. For instance:

<?php
    if($outputDiv1){?>
       <div id="1">Plain old unescaped html code</div>
     <?php }else{ //output div 2 ?>
        <div id="2">More html</div>
     <?php } ?>

I find this method is far easier. If you need at access a php variable from within the html, you simply open up another php block like <input name="name" type="text" value="<?php echo $someVal; ?>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.