[SOLVED] Sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
professorpool42
Forum Newbie
Posts: 2
Joined: Thu Feb 17, 2005 5:01 am

[SOLVED] Sorting

Post by professorpool42 »

Forgive me I am a relative newbie with PHP..

I have constructed a list here:

http://www.jersey.uk8ball.net/rankings.php

I have no problem sorting on any of the columns that feature in the MySQL table, however, I want to sort on the last column that is calculated as follows:

Code: Select all

$totalprizes = $p_proam + $p_open + $p_lsingles + $p_lchamps + $p_oneframe + $p_cabbages + $p_junior + $p_12;
I basically want to sort the table so that the records are displayed in reverse order with the player that has won the most prize money at the top of the list ..

This is the full page code I have constructed so far:

Code: Select all

<?PHP
include("header.php");
include("sidebar.php");

$user="???";
$host="???";
$password="???";
$database="???";

	$connection = mysql_connect($host,$user,$password)
		or die ("Could not connect to the database");
	$db = mysql_select_db($database,$connection)
	    or die ("Could not select the database");

$query = "SELECT * FROM players"; 

$result = mysql_query($query)
	    or die ("Could not execute query");

$rank=1;		

echo "<table cellspacing=3><tr><td><h3>Player Rankings</h3>";
echo "<p>Click on a players name to see further details on each player. Click on an event header to sort by that column</p>";
echo "<table border=1 cellpadding=2><td width=20><b><a href=rankings.php>Rank</a></b></td><td width=200><b><a href=rankings.php?sortt=name>Player</a></td>";
echo "<td width=50><b><a href=rankings.php?sortt=status>Status</a></b></td><td width=50 align=right><b><a href=rankings.php?sortt=proam>Pro Am</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=open>Open</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=ls>Ladies Singles</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=lc>Ladies Champs</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=one>One Frame</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=cab>Cabbages</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=junior>Juniors</a></b></td>";
echo "<td width=50 align=right><b><a href=rankings.php?sortt=125>£125 a man</a></b></td>";
echo "<td width=50 align=right><b>Total</b></td>";

while ($row = mysql_fetch_array($result))

&#123;
   extract($row);
   $totalprizes = $p_proam + $p_open + $p_lsingles + $p_lchamps + $p_oneframe + $p_cabbages + $p_junior + $p_12;
   echo   "<tr><td width=20 align=center>$rank</td>
	        <td width=200><a href=players.php?p_look=".$player_id.">".$player_name."</a></td>\n
			<td width=50>$status</td>\n
			<td width=50 align=right>$p_proam</td>\n
			<td width=50 align=right>$p_open</td>\n
			<td width=50 align=right>$p_lsingles</td>\n
			<td width=50 align=right>$p_lchamps</td>\n
			<td width=50 align=right>$p_oneframe</td>\n
			<td width=50 align=right>$p_cabbages</td>\n
			<td width=50 align=right>$p_junior</td>\n
			<td width=50 align=right>$p_125</td>\n
			<td width=50 align=right>$totalprizes</td>\n";
			$rank = $rank + 1;
&#125;
    echo "</table></td></tr></table>";

include("footer.php");
?>

Pretty please, can anyone put me out of my misery?? :(


feyd | use post formatting tags
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

You can work out the total prize money in the query it self, something like this:

Code: Select all

SELECT ID,player,(kills+deaths) AS total FROM stat_player ORDER BY total DESC
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

x.
Last edited by Maugrim_The_Reaper on Thu Feb 17, 2005 8:18 am, edited 1 time in total.
professorpool42
Forum Newbie
Posts: 2
Joined: Thu Feb 17, 2005 5:01 am

Post by professorpool42 »

That is BRILLIANT, many many thanks!

:D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

please post using the formatting tags we provide such as

Code: Select all

.
Post Reply