1

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']));
3
  • What is the correct order? Could you show the query? Commented Nov 18, 2011 at 8:11
  • Ok will go edit my question now... Commented Nov 18, 2011 at 8:12
  • Note that PDOStatement implements the Traversable interface, so you can loop over results with foreach rather than while. It doesn't affect the order of the results, but is more readable ("for each element of a sequence" rather than "while some condition holds" better describes what the loop does). Commented Nov 18, 2011 at 9:55

3 Answers 3

1

It's absolutely possible:

SELECT social.*, accounts.username 
FROM social 
INNER JOIN accounts 
WHERE social.withID = accounts.ID 
AND (social.id = ? OR social.withID = ?) 
AND type = 'friend'
ORDER BY 
(social.id = $user_id AND social.friendAccept = 0), 
(social.withID = $user_id AND social.friendAccept = 0),
(social.id = $user_id AND social.friendAccept = 1)

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok will try this now, I haven't come across this before, thanks.
Thanks this was so simple and easy, I didn't know that queries could be ordered based on results thanks again bud!
0

You could change the SQL query to do an

ORDER BY x ASC/DESC

Or you could create an array of results in PHP, and then use any of the PHP sort functions to sort the array based on whatever criteria.

See http://php.net/manual/en/array.sorting.php for more info.

Edit: I think

uasort()

might be of help. You can define your own compare function, and PHP will then sort based upon that. See http://www.php.net/manual/en/function.uasort.php for mor info.

Comments

0
$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' ORDER BY social.friendAccept ASC");
$q -> execute(array($user['id'], $user['id']));

ORDER BY social.friendAccept

it will get not accepted users first.

if you give some data example i can be more help

1 Comment

It should be ORDER BY, not ORDERY BY :)

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.