sreekanth Posted August 5, 2008 Share Posted August 5, 2008 hai, iam having an array fetched from database and i want to sort it based on grand total points. this is how it look like name1 rank11 rank12 rank13 rank14 total(total of all ranks in this row) name2 rank21 rank22 rank23 rank24 total( " ). i want to sort this type of array based on total and after that if two members have the same total, then order of arrangement must be based on the maximum value of the individual ranks. for example I have the following two fields name rank1 rank2 rank3 rank4 total name1 3 4 5 4 16 name2 7 3 4 2 16 if i have the records like above then name2 has to come before name1 because it is having the maximum individual value of "7". if you know how to do it,post your answer. thanks in advance. bai.. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/ Share on other sites More sharing options...
lemmin Posted August 5, 2008 Share Posted August 5, 2008 You should order by the total in your query to make things a little easier. $rows = array(); $maxs = array(); while ($row = mysql_fetch_assoc($result)) { $maxs[$row['name']] = max($row['rank1'], $row['rank2'], $row['rank3'], $row['rank4']); $rows[$row['name']]= $row; } $maxs = asort($maxs); foreach ($maxs as $name => $max) echo $rows[$name]['name']; That should work but I may have messed up the logic somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608728 Share on other sites More sharing options...
KevinM1 Posted August 5, 2008 Share Posted August 5, 2008 You should order by the total in your query to make things a little easier. $rows = array(); $maxs = array(); while ($row = mysql_fetch_assoc($result)) { $maxs[$row['name']] = max($row['rank1'], $row['rank2'], $row['rank3'], $row['rank4']); $rows[$row['name']]= $row; } $maxs = asort($maxs); foreach ($maxs as $name => $max) echo $rows[$name]['name']; That should work but I may have messed up the logic somewhere. Wouldn't it be easier to just do the ordering in the query itself? $query = "SELECT * FROM tablename ORDER BY total DESC"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo "{$row['rank1']} {$row['rank2']} ... {$row['total']}"; //everything's ordered by the 'total' column } You can most likely add the other ordering conditions right in the SQL query. Far easier to let the DB do the heavy lifting than to do hack through it in the script. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608750 Share on other sites More sharing options...
lemmin Posted August 5, 2008 Share Posted August 5, 2008 You can't order by the highest of different columns (that I know of). Otherwise, yes, it would be a lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608756 Share on other sites More sharing options...
KevinM1 Posted August 5, 2008 Share Posted August 5, 2008 You can't order by the highest of different columns (that I know of). Otherwise, yes, it would be a lot easier. Sure you can: SELECT * FROM tablename ORDER BY total DESC, rank1 DESC, rank2 DESC ... The above query first orders by the total, then by each column in order. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608778 Share on other sites More sharing options...
lemmin Posted August 5, 2008 Share Posted August 5, 2008 No, that will order by each column individually. Using this example: name rank1 rank2 rank3 rank4 total name1 2 1 1 1 16 name2 1 9 9 9 16 Your query would order name1 first even though name2 has three other ranks that are higher. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608785 Share on other sites More sharing options...
KevinM1 Posted August 5, 2008 Share Posted August 5, 2008 No, that will order by each column individually. Using this example: name rank1 rank2 rank3 rank4 total name1 2 1 1 1 16 name2 1 9 9 9 16 Your query would order name1 first even though name2 has three other ranks that are higher. Ah, good point. Quote Link to comment https://forums.phpfreaks.com/topic/118259-sorting-the-array-ie-fetched-from-mysql-ac-to-position/#findComment-608786 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.