2012-07-05 105 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;