Jump to content

Ordering a table based on another table.....


Recommended Posts

This is a little confusing so please bear with me.  I was thinking of using left.join but couldn't figure out how to implement it properly.

 

I am trying to order all of the results from a table names 'clans' based on how many points the clan has. To calculate the points you have to go into another table 'clanteams' and then loop every team in the clan pulling wins and losses from a row in a specific ladder table.

 

Here is the code i have to calculate the points.

 

	$total[wins] = 0;
$total[loss] = 0;
$members=mysql_query("SELECT id,clanid,teamid,DATE_FORMAT(datejoined,'%M %d, %Y') FROM clanteams WHERE clanid='$member[id]'");
while(list($id,$clanid,$teamid,$joined)=mysql_fetch_row($members)){
	$team=mysql_query("SELECT name,ladderid FROM teams WHERE id='$teamid'");
	if(mysql_num_rows($team) == 0) continue;
	$team=mysql_fetch_array($team);
	$ladder=mysql_query("SELECT name FROM ladders WHERE id='$team[ladderid]'");
	$ladder=mysql_fetch_array($ladder);
	$linfo=mysql_query("SELECT rank,wins,loss FROM ladder_$team[ladderid] WHERE teamid='$teamid'");
	$linfo=mysql_fetch_array($linfo);

	$total[wins] = $total[wins] + $linfo[wins];		
	$total[loss] = $total[loss] + $linfo[loss];

}
$totalpoints = ($total[wins] * 2) - $total[loss];

 

So now i want to loop through every row in the clans table, and using the above code oder them by $total points.

Ive spent hours wrapping my head around it and still cannot figure it out.

 

Please help.

Link to comment
https://forums.phpfreaks.com/topic/221823-ordering-a-table-based-on-another-table/
Share on other sites

Put another field in your database table where the total points is stored.

Then when you compute the total wins, you could update this new field with the value, then you run another query where you pull the clan name, and use ORDER BY total_points ASC or DESC, depending on how you want it..

 

It's probably not the best way to do it, but I'm sure it would work.

 

Denno

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.