1

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:

  1. 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;
    }
    
  2. 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.

2
  • 1
    Please show your query code and your output code. Commented Jun 4, 2011 at 17:16
  • added some info in my answerù Commented Jun 4, 2011 at 17:18

4 Answers 4

2

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

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

8 Comments

and this could be a comment @yes123 instead of an answer.
+1 for speed :) Don't forget to exclude from the last query the user found in the first query, so you don't get it twice.
@coding: he asked how to do the second solution. Isn't that an answer?
@radu: I know it's pretty bad when you waste time when already one posted an answer. Stackoverflow should implement some more advanced LIVE notification when you are writing an answer
@radu: regarding the exclude. You don't need that beacuse UNION isn't UNION ALL :)
|
1

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

1 Comment

@Sebastian I saw that you found a solution to your question, but just wanted to make sure you saw this. It should solve your problem with just a single query without having to do any more work. Here's a tutorial on ordering by specific field values with MySQL: electrictoolbox.com/mysql-order-specific-field-values
1
SELECT *
  FROM USERS
 ORDER BY  (CASE user_id WHEN :currentUserID THEN 1 ELSE 2 END); 

Comments

1

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;

Comments

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.