Php回声,简单的foreach问题

时间:2012-07-04 14:53:42

标签: php

我有这个代码,它回应了数据库中的信息:

<?php
  include('db_connect.php');
  mysql_query("SET NAMES UTF8");

$sql =" select * from hostess";
$query = mysql_query($sql);


    while ($row = mysql_fetch_array($query)) { 
    echo "<div id='photo'>"; 
    echo "<div id='picture'>"; 
     echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
     echo "</div>"; 
echo "<div id='rating'>"; 
     echo "</div>"; 
     echo "<div id='text'>"; 
     echo '<td><a href="hostess.php?id='.$row['id'].'">'. $row['first_name_en']."&nbsp;". $row['family_name_en']."</a></td>";
    echo "</div>"; 
    echo "</div>"; 
    }
    ?>

我还有一个评级系统,我想将其提取到每个ID。我的问题是,我如何为每个id分配每个评级,我的意思是每个评级必须有自己的评级,它们不应该像现在一样显示在一个div中。我需要在每个评级div中显示结果。

我用于所有结果的代码是:

<?php 
// include update.php
include_once 'update.php';
// get all data from tabel
$arr_star = fetchStar();
?>
<?php 
// start looping datas
foreach($arr_star as $star){ ?>
<h2>Star Rater - <?php echo $star['id'];?></h2>
<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>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

尝试(可能有拼写错误,但希望你能得到这个想法)

 switch ($star['id'])
{
 case 1:
 echo '  <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>';
 break;
 case 2: 
 echo '  <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> ']
 break;
 case 3: 
  echo '  <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> '; 
  break;
 case 4:
 echo '  <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> ';
  break;
 case 5:
  echo '  <li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li> ';
 break;
 default :  
  echo 'No vote';
  break;
}