2011-02-18 68 views
0

我试图创建一个if-else语句,它会返回不同的div。我相信这是失败的,因为else语句中有太多'如何php if else声明并返回不同的div?

<?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>' 

} 

?> 

回答

4

当您想要显示条件内容时,请打开并关闭<?php标记,而不要使用echo语句。

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

<div>Hello</div> 

<?php } else { ?> 

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

<? } ?> 

它也将确保您的php代码内的div得到你想要的评估。

0

在JavaScript部分中的'需要进行转义:\',否则他们将通过PHP被看作是你呼应字符串的结尾。

对于像这样的大块文字,请查看HEREDOC s,它允许您输出多行字符串,而不用担心引用转义。

字符串中的同时,你不能嵌入PHP块,你是:

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

将无法​​正常工作。

0

<?php ?>标记内不能有<?php ?>标记。

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

可能做这样的事情:

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

这段代码是所有的地方。

在每行可执行代码的末尾,在语法被认为有效之前,您必须有一个分号(“;”)。当你试图组装PHP和HTML时,没有必要回应的所有内容。以下是您的代码清理版本。如你所见,当你不需要任何PHP逻辑时,你可以用?>来结束php并返回到正常的HTML。你似乎有这个想法,但你把这一切都包含在一个echo声明中。这会起作用,但你有一些<?php echo $whatever; ?>陈述混合在一起。看看我发布的清理代码,你应该能够看到你错在哪里。

<?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 
?> 
0

对于多串是复杂如你所提出的建议之一,我会建议只是在输出之前结束你的PHP代码块。例如:

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

我觉得这个方法要容易得多。如果你需要在html中访问php变量,你只需打开另一个php块,如<input name="name" type="text" value="<?php echo $someVal; ?>" />