如何在另一个循环中创建一个变量循环?

时间:2011-10-23 15:16:01

标签: php html

这可能令人困惑,有点困难。我有一个$fcomm变量,它将值保存为数组。然后将此变量$fcomm分配给另一个foreach循环中的“echo div”。我需要的是让$fcomm循环,同时为每个<div>分配和回显。这是代码......感谢您的任何评论。

PHP:

for ($i2=0; $i2<$rowscheck; $i2++) {

  //FRIEND QUERY COMMENTS
  $fr_com = mysqli_query($connect,"SELECT * FROM comments WHERE name_main_id = '".$fcom[$i2]."' ORDER BY comm_id ASC ");

  while ($rows_com = mysqli_fetch_array($fr_com)) {
    extract($rows_com); 
    $fcomm[] = $rows_com['comment_main'];
  }

} 

if ($fr_check > 0 ) { 
  foreach ($friends_q2 as $fr_ids) {
    $added_fr = "members/$fr_ids/userImg1.jpg";
    if (!file_exists($added_fr)) { 
      $added_fr =  "members/avatar/avatar.png" ; 
    } 
    echo "
<div id='frslide'>
  <a href='javascript:window_usr($fr_ids)'>
    <img src='".$added_fr."' height='68' width='66' hspace='2' vspace='16' id='fadd'/>
  </a>
  <span style='font-size:12px;position:relative;left:-71px;top:-1px;color:#ffffff; background-image:url(images/back_bar.png);'>&nbsp;".$frnames2."&nbsp;</span>
</div>
<div id='frdiv' class='frdiv'>
  <span style='font-size:12px;position:relative; left:-1px;top:72px;color:#ffffff;background-image:url(images/usr_main.png);'>
    &nbsp;<a href='javascript:remusr($fr_ids)'>remove</a>&nbsp;
  </span>
</div>
<div>".$fcomm;  
  }
}

$fcomm变量包含SQL的注释字符串。因此,当我将$fcomm[$i]或任何循环变量添加到$fcomm时,它只会产生来自注释的单个字母 - 我需要的是让$fcomm打印整个字符串但不同的字符串和对应的字符串适当的<div>。当我试图将$fcomm置于内部循环中时 - 它会打印字符串,但每个字符串都是相同的......

2 个答案:

答案 0 :(得分:3)

您需要增加您尝试从fcomm数组中检索的索引。请注意使用$ j变量的更改。

if ($fr_check > 0 ) { 
  $j=0;
  foreach ($friends_q2 as $fr_ids) {
    $added_fr = "members/$fr_ids/userImg1.jpg";
    if (!file_exists($added_fr)) { 
      $added_fr =  "members/avatar/avatar.png" ; 
    } 
    echo "
<div id='frslide'>
  <a href='javascript:window_usr($fr_ids)'>
    <img src='".$added_fr."' height='68' width='66' hspace='2' vspace='16' id='fadd'/>
  </a>
  <span style='font-size:12px;position:relative;left:-71px;top:-1px;color:#ffffff; background-image:url(images/back_bar.png);'>&nbsp;".$frnames2."&nbsp;</span>
</div>
<div id='frdiv' class='frdiv'>
  <span style='font-size:12px;position:relative; left:-1px;top:72px;color:#ffffff;background-image:url(images/usr_main.png);'>
    &nbsp;<a href='javascript:remusr($fr_ids)'>remove</a>&nbsp;
  </span>
</div>
<div>".$fcomm[$j];  
    $j++;
  }
}

答案 1 :(得分:1)

在不知道您的代码或列名等的情况下,您可能需要根据朋友ID填充$fcomm,然后在foreach循环中回显相应的注释。

这样的事情:

for ($i2=0; $i2<$rowscheck; $i2++) {

    //FRIEND QUERY COMMENTS
    $fr_com = mysqli_query($connect,"SELECT * FROM comments WHERE name_main_id =  
             '".$fcom[$i2]."' ORDER BY comm_id ASC ");

    while ($rows_com = mysqli_fetch_array($fr_com)) {
        extract($rows_com); 
        // populate $fcomm sub-array using the ID used to select 
        // them, i.e. $fcom[$i2]
        $fcomm[ $fcom[$i2] ][] = $rows_com['comment_main'];    
    }
} 

if ($fr_check > 0 ) { 

    foreach ($friends_q2 as $fr_ids) {

        $added_fr = "members/$fr_ids/userImg1.jpg";

        if (!file_exists($added_fr)) { 
            $added_fr =  "members/avatar/avatar.png" ; 

        } 

        echo "<div id='frslide'>...</div>";

        // echo out the right $fcomm depending on the ID, $fr_ids
        foreach ($fcomm[$fr_ids] as $comment) {
            echo '<div>', $comment, '</div>';  
        }
    }
}