1

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

5
  • 1
    Your code and markup formatting is error prone. Work on indenting more consistently. Commented Oct 23, 2011 at 15:17
  • 2
    extract($rows_com); is a great way to introduce bugs. Let's not do that. Commented Oct 23, 2011 at 15:24
  • @ThiefMaster No probs, it doesn't play a role in here it can be commented out.. Commented Oct 23, 2011 at 15:25
  • I don't understand what "make a variable loop itself inside another loop" means Commented Oct 23, 2011 at 15:53
  • $fcomm should loop through itself while it's placed inside foreach loop like in here... Commented Oct 23, 2011 at 17:13

2 Answers 2

3

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++;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great ! I missed $j=0; Works prefect now...Thanks a lot for your help, well done, you deserve lovely award points...Also, thanks guys as usual to the other answers and your help..
1

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

4 Comments

I'm not sure what you intend, to be honest. You're populating $fcomm with more than one set of data (i.e. via $i2 as well as putting in the query results). So I don't actually know what the keys should be.
I've got a table with comments - the comments are queried alongside the divs with each user's pics - the comments should be aligned to the pics in a div. I have proper pics and divs displays but I cannot assign a proper comment to each pic/div. I only succeed in printing one comment or a string of the same comment repeating..
Maybe you need to set $fcomm[$fcom[$i2]][] = $rows_com['comment_main']; as an array of arrays, and then after the echo "<div></div>";, have a foreach ($fcomm[$fr_ids] as $comment) { echo '<div>', $comment, '</div>'; }. I'm still not entirely sure what you're trying to do, I'm afraid. Will update the answer above with this suggestion though, so it's clearer.
Thanks a lot Daren! I'll mind your code - It's been sorted out now - Set Sail Media's solution is exactly what I was looking for...cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.