I have a while loop that I use to echo multiple mysql results that translate into offline users. To make things more organized, I wanted to perform the query, store the results in a variable and then echo it at the bottom of the page. I need to echo both online and offline users within a certain parent div, so it seems cluttered to me to echo the first div tag and then perform both queries and echo the results, then echo the closing tag. Currently, if I try to echo the result from outside the while loop, I only get 1 result. If anyone has any ideas, I would appreciate it.
$sql = 'SELECT * FROM users
LEFT JOIN friendships
ON friendships.friend_id = users.id
WHERE friendships.user_id = ?
AND users.id NOT IN (
SELECT active_users.id FROM active_users)';
$stmt5 = $conn->prepare($sql);
$result=$stmt5->execute(array($userid));
while ($row = $stmt5->fetch(PDO::FETCH_ASSOC)) {
$online=htmlspecialchars( $row['username'], ENT_NOQUOTES, 'UTF-8' );
$online = "<div class='user-online'>
<a data-name=\"$to\">$to</a>
</div>";
$online.=etc...plus do other processes
}
<div id=\"online\">
".$online."
</div>
You need to store your SQL results in an array
I’ve trimmed some code for clarity but hopefully it helps