I'm using a row to return all of the users for a particular project, which isn't a problem. However, how would I order it so that the first result is always the logged in user.
I see two solutions, both of which I've tried with no success:
Printing the user's name then using an if statement to only print the rest of the row if they are different from the user's name.
print user's name;
if (username !== user's name) {
print this name;
}
Printing them all in a row but ordering it somehow so that the current user is at the top.
Any ideas?
EDIT
I'm trying it now with the following code:
$query7 = mysql_query("SELECT users_ID FROM projects_users WHERE projects_id = '$id' AND WHERE users_id <>'$q6[0]'") or die(mysql_error());
But I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE users_id <>'1'' at line 1
I've tried != and NOT instead of <> but I can't see what's going wrong!
Never mind, fixed it. For future reference, don't sure WHERE twice ;)
Thanks for your help everyone.
You first solution is perfect.
If you want to use your second solution you should build your SQL query like this:
(SELECT * FROM user WHERE id = :currentUserID) UNION (SELECT * FROM user)
And you are good
I think you could do this with a simple mysql query:
SELECT * FROM table_name ORDER BY FIELD(username, 'username');
To learn more: http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
if you want to use php than, You can try something like this
$currentuser = "";
$otherusers = "";
if(username == user's name)
{
$currentuser = username;
}
else
{
$otherusers .= username;
}
echo $currentuser;
echo $otherusers;
SELECT *
FROM USERS
ORDER BY (CASE user_id WHEN :currentUserID THEN 1 ELSE 2 END);