|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
php sorting help
For all the MySql quaries i've made using php i've been able to just order the data to be displayed using ORDER BY whatever, but now i need to put a list in order where a football team with the most points appears at the top of the list and the team with the lowest points at the bottom. I've read about rsort in the php manual but none of the examples seem to be related to what i need to do. If someone could help me out by telling me what code i need and where i need to put it that would be great. Below is my code and any help would be greatly appreciated. Sorry if this question seems a bit dumb but i'm still just a newbie at this.
$table_name = "$age"; $connection = @mysql_connect("$host", "$user", "$pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $sql = "SELECT team, played, won, draw, lost, gf, ga, points, gd FROM $table_name ORDER BY points "; $result = @mysql_query($sql,$connection) or die("Couldn't execute query."); $num=mysql_numrows($result); $i=0; while ($i < $num) { $team=mysql_result($result,$i,"team"); $played=mysql_result($result,$i,"played"); $won=mysql_result($result,$i,"won"); $draw=mysql_result($result,$i,"draw"); $lost=mysql_result($result,$i,"lost"); $gf=mysql_result($result,$i,"gf"); $ga=mysql_result($result,$i,"ga"); $points=mysql_result($result,$i,"points"); $gd=mysql_result($result,$i,"gd"); $bgcolor = ($bgcolor == "#008000" ? "#06A806": "#008000"); ?> <tr <? echo "bgcolor=$bgcolor" ?>> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$team"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$played"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$won"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$draw"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$lost"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo " $gf "; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$ga"; ?></font></td> <td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$points"; ?></font></td> </tr> <? ++$i; } MYSQL_CLOSE(); ?> |
|
#2
|
|||
|
|||
|
That is almost right, you just forgot DESC at the end of the order, to get the highest to lowest, right now its lowest to highest
plus ive rewriten your code as there is a more efficient way to do this PHP Code:
Just to let you know what ive done, ive added DESC to your sql query. removed $num=mysql_numrows($result); $i=0; while ($i < $num) { $team=mysql_result($result,$i,"team"); $played=mysql_result($result,$i,"played"); $won=mysql_result($result,$i,"won"); $draw=mysql_result($result,$i,"draw"); $lost=mysql_result($result,$i,"lost"); $gf=mysql_result($result,$i,"gf"); $ga=mysql_result($result,$i,"ga"); $points=mysql_result($result,$i,"points"); $gd=mysql_result($result,$i,"gd"); $bgcolor = ($bgcolor == "#008000" ? "#06A806": "#008000"); if you want it to return more then one row (multidimention array) then simply use while ($row = mysql_fetch_array($query)) its much more efficient, mysql_result places a big load on the server is your calling it that many times, its much more efficient to use $row if your echo'ing a var you dont need to place it in "$var". you can just echo it like this echo $var; finally, <? echo $row["gf"]; ?> because $row is now an array, you can call it like this |
|
#3
|
|||
|
|||
|
Thanks alot Ben for your help. It's great when someone takes the time to not only reply to a plea for help but also shows better ways to acheive the same results. It really does make the learning process easier.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > php sorting help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|