tags:

views:

28

answers:

2

how do I order this result??

$range = 5;  // you'll be selecting around this range. 
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want. 

$result = mysql_query("select * from table where rank between $min and $max limit $limit");

while($row = mysql_fetch_array($result))
{
   echo $row['name']."&nbsp;-&nbsp;".$row['rank']."<br>";
}
+2  A: 
$result = mysql_query(
    "select * from table where rank between $min and $max " . 
    "order by rank asc limit $limit"
);
Artefacto
+1 for using . to make it readable.
egrunin
A: 

Use the "order by"- clause:

mysql_query("select * from table where rank between $min and $max order by rank limit $limit");

This will order your result from little to big values. Use "order by rank desc" to order in descendent direction. (big -> little)

Simon
thanks but how can I display search query in middle and rest is above and below example:seach is mathewand the result should be display below:-john - 26joe - 25stewart - 27mathew - 25kelly - 24brandon -23magy - 22 .......etc.
mathew
Use 2 select clauses: The one looking for values between "rank - range" and "rank" and the other looking for values between "rank + 1" and "rank + range". Order the first in ascending and the second in descending order.
Simon
OOps my bad i got the answer. actually it is more easier than that.only thing needs to do is "select * from table where rank between $min and $max order by rank desc limit $limit"thats it...
mathew