I have the following code for displaying invites sent, received and accepted for a simple friends system.
while ($friend = $q -> fetch(PDO::FETCH_ASSOC)) {
if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have sent a request to be <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '\'s</a> friend.</p>';
if ($friend['withID'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have recieved a request from <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 1) echo '<p>You are friends with <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
}
It will display what friends you have got, what friend requests you have sent, and what friend request have been received. I get all of this from one mysql query. But the only problem being they are not in order as the while loop iterates itself down the list and does whatever it is supposed to do.
Is there a way of getting them in order while still only using one query? By order I mean all the requests received in one list, requests sent in one list, and friends in one list. As it is at the minute they are in the order they are pulled from the table.
The query…
$q = $dbc -> prepare("SELECT social.*, accounts.username FROM social INNER JOIN accounts WHERE social.withID = accounts.ID AND (social.id = ? OR social.withID = ?) AND type = 'friend'");
$q -> execute(array($user['id'], $user['id']));
It’s absolutely possible:
Just put the ORDER BY clause in the correct order. The way this works, it orders it using boolean values that the expressions evaluate to.
Of course, you should also convert $user_id to ? in the prepared statement.