2012-07-05 41 views
-3

我有了這個簡單的問題的方式的變化:PHP的echo,打印

我有這樣的代碼是所有好的,但我需要的是打印它以不同的方式,我嘗試了的東西,但問題是,我沒有得到任何錯誤,但結果不走正道,

我想要打印的代碼是這樣的:

<ul class='star-rating' id="star-rating-<?php echo $star['id'];?>"> 
<?php /* getRating($id) is to generate current rating */?> 
    <li class="current-rating" id="current-rating-<?php echo $star['id'];?>" style="width:<?php echo getRating($star['id'])?>%"><!-- will show current rating --></li> 
    <?php 
    /* we need to generate 'id' for star rating.. this 'id' will identify which data to execute */ 
    /* we will pass it in ajax later */ 
    ?> 
    <span class="ratelinks" id="<?php echo $star['id'];?>"> 
    <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li> 
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li> 
    <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li> 
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li> 
    <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li> 
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li> 
    <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li> 
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li> 
    <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li> 
    </span> 
</ul> 

雖然我」 m試試這個:

echo"<ul class='star-rating' id='star-rating-'". $star['id']."> 
<li class='current-rating' id='current-rating-". $star['id']." style='width:". getRating($star['id'])."'%></li>"; 
echo'<span class="ratelinks" id="'. $star['id'].'"> 
    <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li> 
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li> 
    <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li> 
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li> 
    <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li> 
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li> 
    <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li> 
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li> 
    <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li> 
    </span> 
</ul>'; 
+0

_「結果不正確」_ - 然後轉過來。或者解釋真正的問題。我想你必須逃避報價。 – CodeCaster 2012-07-05 09:12:28

回答

3

你錯過了echo和字符串之間的空格。你在字符串中也有一些錯誤。

echo "<ul class='star-rating' id='star-rating-". $star['id']."'> 
<li class='current-rating' id='current-rating-". $star['id']."' style='width:". getRating($star['id'])."%'></li>"; 
echo '<span class="ratelinks" id="'. $star['id'].'"> 
    <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li> 
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li> 
    <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li> 
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li> 
    <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li> 
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li> 
    <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li> 
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li> 
    <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li> 
    </span> 
</ul>'; 

我必須說我很想知道爲什麼要做這個改變的原因嗎? 我建議用單引號'附上你的字符串。通過這種方式,您仍然可以添加帶有雙引號"的HTML屬性,而不必轉義它們。

例子:

echo '<ul class="star-rating" id="star-rating=' . $star['id'] . '"> 
    <li class="current-rating" id="current-rating-' . $star['id'] . '" style="width:' . getRating($star['id']) . '%"></li>'; 
2

當大塊的HTML處理我總是建議使用heredoc,並使用{}在變量substitue,即{$star['id']}

您需要在此heredoc之前計算您的星級。

echo <<<"EOD" 
<ul class='star-rating' id="star-rating-{$star['id']}"> 
<?php /* getRating($id) is to generate current rating */?> 
    <li class="current-rating" id="current-rating-{$star['id']}" style="width:{$star_rating['id']}%"><!-- will show current rating --></li> 
    <span class="ratelinks" id="<?php echo $star['id'];?>"> 
    <li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li> 
    <li><a href="javascript:void(0)" title="1 star and a half out of 5" class="one-star-half">1.5</a></li> 
    <li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li> 
    <li><a href="javascript:void(0)" title="2 star and a half out of 5" class="two-star-half">2.5</a></li> 
    <li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li> 
    <li><a href="javascript:void(0)" title="3 star and a half out of 5" class="three-star-half">3.5</a></li> 
    <li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li> 
    <li><a href="javascript:void(0)" title="4 star and a half out of 5" class="four-star-half">4.5</a></li> 
    <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li> 
    </span> 
</ul> 
EOD;