doupingyun73833 2011-10-23 15:16
浏览 125
已采纳

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

It might be confusing and a bit difficult. I have a $fcomm variable that holds values as an array. This variable $fcomm is then assigned to an 'echo div' into another foreach loop. What I need is to make $fcomm loop itself while it gets assigned and echoed with each <div>. Here's the code...Thank you for any comments.

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 variable holds strings of comments from SQL. So when I add $fcomm[$i] or any loop variable to $fcomm it yields just single letters from comments - all I need is to make $fcomm print whole strings but different ones and the ones that correspond to proper each <div>. When I tried to place $fcomm in an inside loop - it prints strings but each string is the same...

  • 写回答

2条回答 默认 最新

  • douxieti6851 2011-10-23 16:20
    关注

    You need to increase the index you're trying to retrieve from the fcomm array. Note the changes using $j variable.

    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条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作