0

I have this code which produces me a number which is equal to the number of id's i've got in my database of star rating system.

This code generates me a five star voting for each id i've got, but the problem is, it generates them all in a div, while i need them specifically in different div's. let's suppose i print out in a div information for each hostess i've got, i print out their photo and name with the following code:

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

Now, i've got this other php which generates me all the star ratings for all hostess id's

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

What i need is to assign each hostess profile i print their system rating. I try to insert the foreach inside the first script but it then shows me just one profile, not all profiles.

The fetchstar() code is:

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

1 回答 1

0

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

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

循环通过 MySQL left join in php vs. 2个单独的查询

$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
   }
}
于 2012-07-05T08:56:46.107 回答