knz Posted March 19, 2009 Share Posted March 19, 2009 Hi! I got a problem with making my db-connected table so sort right. It's a table containing a highscore list for a trivia i have made. I want it to sort the table after, for example, the number of correct answers with the highest at the top. I tried to fix this by using this code but without any luck: $sql = "SELECT TableName FROM highscores GROUP BY TableName ORDER BY QuestionsCorrect"; I have tried both ASC and DESC. Here is the whole code including the query: http://pastebin.com/m193ca5fa Here is the sql-file exported from highscores: http://pastebin.com/m4e31b7fe And here is a picture of what it looks like now: http://sv.tinypic.com/view.php?pic=2n824xi&s=5 The site may contain more than 1 trivia it's grouping the different games (TableName) so that the scored wont get in the same table. Im new with both php and mysql so im looking for the easiest solution as possible, and if you have som tips of changes please write them in a "for dummies" kind of way. Thank you! // K Quote Link to comment https://forums.phpfreaks.com/topic/150175-trouble-trying-to-sort-table-using-order-by/ Share on other sites More sharing options...
Scorpy Posted March 19, 2009 Share Posted March 19, 2009 I do a different method with doing a ranking system, see below. SET @iCurUserRank=0 UPDATE users SET user_rank=(SELECT @iCurUserRank:=@iCurUserRank+1) ORDER BY user_score DESC, user_name ASC That assigns the user_rank in order of the score, if two scores match then by their name. Then it's just a select statement to fetch the ranks. Something like this might be useful to you with some tweaking to how you want, just requires another column (or more if you want to do several). Quote Link to comment https://forums.phpfreaks.com/topic/150175-trouble-trying-to-sort-table-using-order-by/#findComment-788688 Share on other sites More sharing options...
knz Posted March 19, 2009 Author Share Posted March 19, 2009 I do a different method with doing a ranking system, see below. SET @iCurUserRank=0 UPDATE users SET user_rank=(SELECT @iCurUserRank:=@iCurUserRank+1) ORDER BY user_score DESC, user_name ASC That assigns the user_rank in order of the score, if two scores match then by their name. Then it's just a select statement to fetch the ranks. Something like this might be useful to you with some tweaking to how you want, just requires another column (or more if you want to do several). Hey thanks, but im not sure that I have the knowledge to make that procedure. I dont know what code should be changed and how? Quote Link to comment https://forums.phpfreaks.com/topic/150175-trouble-trying-to-sort-table-using-order-by/#findComment-788765 Share on other sites More sharing options...
fenway Posted March 27, 2009 Share Posted March 27, 2009 You were just given the code... Quote Link to comment https://forums.phpfreaks.com/topic/150175-trouble-trying-to-sort-table-using-order-by/#findComment-795245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.