-3

私はこの簡単な質問があります:

私はすべて大丈夫なこのコードを持っています、しかし私が必要とするのはそれを別の方法で印刷することです、そして私は何かを試しました、しかし問題は私がエラーを受け取らないということです、しかし結果は正しく行きません仕方、

私が印刷しようとしているコードはこれです:

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

私がこれを試している間:

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>';
4

2 に答える 2

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>';
于 2012-07-05T09:12:07.957 に答える
2

HTML の大部分を扱うときは、常に変数heredocを使用して置換することをお勧めします。{}{$star['id']}

このヒアドキュメントの前に星評価を計算する必要があります。

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;
于 2012-07-05T09:16:05.617 に答える