【问题标题】:Php foreach echo issuephp foreach 回声问题
【发布时间】:2012-09-29 23:46:53
【问题描述】:

我有这个代码,它产生的数字等于我在我的星级评分系统数据库中获得的 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;
}

【问题讨论】:

  • 如果你摆脱了@,那么你的错误就不会被抑制,所以你可以看到发生了什么。
  • 你发布了与stackoverflow.com/q/11331423/687262 几乎完全相同的代码,以至于你甚至称它为相同的东西,这不是很有趣
  • 嗨,Bugfinder,对不起,我们在做同样的事情,所以可能是的,那是相同的代码!
  • 听起来好像出了点问题,你是如何检查你有多个个人资料的?它是作为家庭作业的一部分的预定义数据吗?
  • 请不要使用 mysql_* 函数。它们不再被维护,并且除了名称之外都被弃用了。请切换到更现代的数据库驱动程序,例如 mysqli 或 PDO。

标签: php mysql


【解决方案1】:

首先,您可能不应该使用 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
   }
}

【讨论】:

    猜你喜欢
    • 2010-11-29
    • 2011-09-11
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2017-05-18
    • 2011-07-22
    • 2021-12-27
    相关资源
    最近更新 更多