Page 1 of 1

Sorting results from variables after a query

Posted: Sun Jul 31, 2005 10:53 am
by phase
hi all, i think im going to strggle even explaining what im trying to achieve here, but ill give it my best shot.

im trying to get some ids from a db table, then do some further queries which then determines some values stored in variables. I would then like to order my sql results based on those variables.

Here is the code, ive tried to document as much as possible to give you an idea...

Code: Select all

//i firstly need to get the ids of each team playing in the league and division (variables set in url)
			$getclans = mysql_query("SELECT clan_id FROM clans_div WHERE league_id = $league AND div_id = $div");
			while ($myrow = mysql_fetch_array($getclans)){
				  $clan = $myrow["clan_id"];
			
				  //these are functions to find out the results of matches from a fixtures table, they work fine and not a problem
				  $played = ClanPlayed($clan); // i.e played 4
				  $won = ClanWin($clan); // i.e won 2
				  $lost = ClanLost($clan); // i.e lost 1
				  $drawn = ClanDraw($clan); // i.e Drawn 1
				  $points = ($won * 3 + $drawn * 1); // i.e points 7

				  //i need to order these results based on the above variables, at the moment they are ordering by the first sql query, the data contained is correct, but the ordering incorrect.
				   echo "<a href=\"index.php?view=leagues&clan=$clan\">$clan</a> Played $played Won $won Lost $lost Drawn $drawn Points $points<br>";
}
Hopefully you can see what im trying to achieve, i need tobe able to display the results based on the later variables opposed to ordering them by the initial sql query.

Any ideas of how to do this woul dbe greatly appreciated.

Thanks

Phase

Posted: Sun Jul 31, 2005 11:29 am
by timvw
The rows in a resultset are unordered per defintion. You can order them by using a WHERE clause.

Fe: ORDER BY league_id, div_id

Posted: Sun Jul 31, 2005 11:38 am
by phase
thats the thing, i need to ORDER BY $points but the $points variable is made by php after the query, and there is no db column to do a where clause, all my sql query does is get the team ids, then php goes on to create the points, won , lost etc.

so i need to order by those variables. :(

Posted: Sun Jul 31, 2005 11:58 am
by timvw
Well, have a look at the http://www.php.net/array sort functions...
(But i have a feeling you could do the calculation of points in your query too..)

Posted: Tue Aug 02, 2005 11:09 am
by phase
hi again,

i coul dreally use someone to talk through my design of this code, 3 days ive spent looking at this code and trying varying methods and i am not one step further, i really need to revise how i am approaching it...

if anyone would be kind enough to chat to me on msn and go through this with me i would be extremely grateful to say the least.

my msn is se_phase@hotmail.com if anyone can do this.

thanks for your time.

Phase

Posted: Tue Aug 02, 2005 7:53 pm
by harrisonad
bool arsort ( array &array [, int sort_flags] )

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.
You are lucky you have someone to read the manual for you. :roll: