Jump to content

Recommended Posts

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..

 

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.

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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.