2011-02-18 66 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; ?>" />