Php foreach echo问题

时间:2012-07-05 08:20:37

标签: php mysql

我有这个代码,它产生的数字等于我在星级评定系统数据库中的id数。

这个代码为我生成的每个id生成五星投票,但问题是,它在div中生成它们,而我需要它们专门用于不同的div。让我假设我打印出每个女主人的div信息,我用下面的代码打印出他们的照片和名字:

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


while ($row = mysql_fetch_array($query)) { 

echo "<div id='photo'>";

echo "<div id='picture'>"; 
echo "<div id='scotch'><img src='images/Scotch.png'></div>"; 
 echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
 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>";
echo "<div id='photo2'>"; 
echo "<div id='picture'>";

echo "<div id='notes'>";
echo '<form action="index.php" method="post" >'; 
 echo "<label>Notes</label></br><textarea>".$row['courrent_occupation'] . "</textarea></br>";
  echo '<input type="submit" value="edit" name="edit"></div>';


 echo "</div>"; 
 echo "<div id='notes'>"; 
 echo "<label>profile</label></br><textarea>".$row['profile_en'] . "</textarea>";
echo "</div>"; 
echo "</div>"; 
}
?>
</div>

现在,我已经有了这个其他的php,它为我生成了所有女主人id的所有星级评分

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

我需要的是分配每个女主人简档我打印他们的系统评级。 我尝试在第一个脚本中插入foreach,但它只显示一个配置文件,而不是所有配置文件。

fetchstar()代码是:

function fetchStar(){
    $sql = "select * from `hostess`";
    $result=@mysql_query($sql);
    while($rs = @mysql_fetch_array($result,MYSQL_ASSOC)){
        $arr_data[] = $rs;
    }
    return $arr_data;
}

1 个答案:

答案 0 :(得分:0)

首先,您可能不应该使用SELECT *。除此之外,我将结合你所拥有的两个查询来返回一个带有MySQL的多维数组,然后使用嵌套的每个循环来回显你想要的数据。

有人在这里为我回答了类似的问题。

Looping through MySQL left join in php vs. 2 separate queries

$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) { 
        if ($lastID <> $row['id']) {
            $lastID  = $row['id'];
             $hostess[$lastID] = array('id' => $row['id'],
                                  'first_name_en' => $row['first_name_en'],
                                  etc
                                  'arr_star' => array() );
}
$hostess[$lastID]['arr_star'][] = array('star_id' => $row['star_id'] etc);
}

然后你会为每个语句使用嵌套

for each($row as $rows){
      //echo your hostess information

  for each ($arr_star as $star){
      //echo your star rating information
   }
}