Sorting results from variables after a query

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
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Sorting results from variables after a query

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post 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. :(
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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..)
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post 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
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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:
Post Reply