Rod Rig Gez
Rod Rig Gez

Reputation: 133

How to make a variable loop itself inside another loop?

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

Upvotes: 1

Views: 249

Answers (2)

Daren Chandisingh
Daren Chandisingh

Reputation: 2165

Without knowing your code or column names etc., you probably need to populate $fcomm based on the friend IDs, and then echo out the corresponding comment in the foreach loop.

Something like this:

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

Upvotes: 1

Patrick Moore
Patrick Moore

Reputation: 13344

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

Upvotes: 3

Related Questions