PHP foreach回声问题 [英] Php foreach echo issue

查看:46
本文介绍了PHP foreach回声问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,它为我生成一个数字,该数字等于我在星级评定系统数据库中的 ID 数量.

这段代码为我获得的每个 id 生成一个五星投票,但问题是,它在一个 div 中生成它们,而我在不同的 div 中特别需要它们.让我们假设我在一个 div 信息中为我得到的每个女主人打印出来,我用以下代码打印出她们的照片和姓名:

$sql =" select * from hostess";$query = mysql_query($sql);while ($row = mysql_fetch_array($query)) {回声<div id='照片'>";echo "<div id='图片'>";echo "<div id='scotch'><img src='images/Scotch.png'></div>";echo "<td> <img src=foto/photo1/".$row['photo'] ."></td>";echo "</div>";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='图片'>";echo "

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

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

现在,我有另一个 php,它为我生成所有女主人 id 的所有星级

<?php//开始循环数据foreach($arr_star 作为 $star){ ?><h2>星级评分 - <?php echo $star['id'];?></h2><ul class='star-rating' id="star-rating-<?php echo $star['id'];?>"><?php/* getRating($id) 是生成当前评分 */?><li class="current-rating" id="current-rating-<?php echo $star['id'];?>"style="width:<?php echo getRating($star['id'])?>%"><!-- 将显示当前评级 --></li><?php/* 我们需要为星级评分生成id"..这个id"将标识要执行的数据 *//* 稍后我们将在 ajax 中传递它 */?><span class="ratelinks" id="<?php echo $star['id'];?>"><li><a href="javascript:void(0)" title="1 星,共 5 颗" class="one-star">1</a></li><li><a href="javascript:void(0)" title="5 星中的 1 星半" class="one-star-half">1.5</a></li><li><a href="javascript:void(0)" title="2 颗星,共 5 颗星" class="两颗星">2</a></li><li><a href="javascript:void(0)" title="2 星半(满分 5 分)" class="two-star-half">2.5</a></li><li><a href="javascript:void(0)" title="3 颗星,共 5 颗星" class="三颗星">3</a></li><li><a href="javascript:void(0)" title="5 分之三星半" class="三星半">3.5</a></li><li><a href="javascript:void(0)" title="4 颗星,共 5 颗星" class="四颗星">4</a></li><li><a href="javascript:void(0)" title="5 分之四星半" class="四星半">4.5</a></li><li><a href="javascript:void(0)" title="5 颗星,共 5 颗星" class="五颗星">5</a></li></span><?php } ?>

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

fetchstar() 代码是:

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

解决方案

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

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

遍历 MySQLphp 中的左连接 vs. 2 个单独的查询

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

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

for each($row as $rows){//回显你的女主人信息对于每个($arr_star 作为 $star){//echo你的星级信息}}

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

解决方案

First, you probably shouldn't use SELECT *. That aside I would combine the two queries you have to return a multidimensional array with MySQL and then use nested for each loops to echo out the data you want.

Someone answered a similar question for me here.

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);
}

Then you would use nested for each statements

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

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

这篇关于PHP foreach回声问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆