Hello, how can I output from the database and display all this data outside the while zone?

There are 52456 lines in the database, you need to output everything and display it in a block, which is located behind while.

$usersfriendsmini = mysql_query("SELECT * FROM users WHERE (id='$friendusmini[idUser]' OR id='$friendusmini[idFriend]') AND id not like '%$_GET[id]%'"); while ($userfriendmini = mysql_fetch_array($usersfriendsmini)) { if ($userfriendmini[avatar] == '0') { $userfriendminiav = 'user.png'; } else { $userfriendminiav = "avatars/".$userfriendmini[avatar]; } $friendbl = "<a href='/profile/".$userfriendmini[id]."/'><img src='/images/".$userfriendminiav."' title='".$userfriendmini[surname]." ".$userfriendmini[name]."' class='avatar'></a>"; } <div class='test'><?php echo $friendbl; ?></div> 

The problem is precisely that it does not display everything, but only one record, or rather the first one, which is displayed. I need to get everything out.

Tried without while, but without while it is only 1 record and displays ...

I think the problem is due to the fact that it is in a variable, since the variable grabs the first record and outputs only it. Maybe you can somehow replace it?

    3 answers 3

    How about buffering the output stream :

     <?php ob_start(); $usersfriendsmini = mysql_query("SELECT * FROM users WHERE (id='$friendusmini[idUser]' OR id='$friendusmini[idFriend]') AND id not like '%$_GET[id]%'"); while ($userfriendmini = mysql_fetch_array($usersfriendsmini)) { if ($userfriendmini[avatar] == '0') { $userfriendminiav = 'user.png'; } else { $userfriendminiav = "avatars/".$userfriendmini[avatar]; } echo "<a href='/profile/".$userfriendmini[id]."/'><img src='/images/".$userfriendminiav."' title='".$userfriendmini[surname]." ".$userfriendmini[name]."' class='avatar'></a>"; } $friendbl = ob_get_contents(); ob_end_clean(); ?> <div class='test'><?php echo $friendbl; ?></div> 

      So you rewrite the value of the $ friendbl variable at each iteration of the loop. Save the entries in the array, i.e. each new element of the array is one entry.

       $usersfriendsmini = mysql_query("SELECT * FROM users WHERE (id='$friendusmini[idUser]' OR id='$friendusmini[idFriend]') AND id not like '%$_GET[id]%'"); $friendbl = array(); while ($userfriendmini = mysql_fetch_array($usersfriendsmini)) { if ($userfriendmini[avatar] == '0') { $userfriendminiav = 'user.png'; } else { $userfriendminiav = "avatars/".$userfriendmini[avatar]; } $friendbl[] = "<a href='/profile/".$userfriendmini[id]."/'><img src='/images/".$userfriendminiav."' title='".$userfriendmini[surname]." ".$userfriendmini[name]."' class='avatar'></a>"; } 

      Output the same way through the loop:

       foreach ($friendbl as $v) { echo $v; } 

        Place echo $friendbl at the end of the loop.

        • This is not a solution to the problem. I need this conclusion to be after all this. Since I am now building php from above, and html from below - Sasha Osipov
        • Then maybe $friendbl = $friendbl + "<a href='/profile/".$userfriendmini[id]."/'><img src='/images/".$userfriendminiav."' title='".$userfriendmini[surname]." ".$userfriendmini[name]."' class='avatar'></a>"; - Anatol
        • It works, but the output is not correct ... prntscr.com/c5swex - Sasha Osipov
        • So. Understood. Instead of "+" put "." - Sasha Osipov
        • @ SashaOsipov Use the abbreviated entry "$friendbl .= "строка"; instead of $friendbl = $friendbl . "строка"; code will be easier to read. String operators - Gleb Kemarsky