Sort.
Posted: Tue Apr 19, 2005 1:05 pm
Hi all, I have this chunk of code that grabs baseball scores out of a db. Right now when it sorts, it gives higher priority to the team with more games... For example, if the db has the following in it:
It would show team b first when the win % of team a is actually better. Make sense? I want it to sort by win%.
Here is the code, any ideas are greatly appreciated!
feyd | Please review how to post code using
Code: Select all
teama 2 wins 0 loss and 0 ties
teamb 2 wins 1 loss and 0 tiesHere is the code, any ideas are greatly appreciated!
Code: Select all
<?
if (isset($submit_league) and isset($league) and $season!='' and $league!=''){
echo $league." - ".$season. "<br>";
if ($season=='First_half'){
$cond = "< '$day'";
}
if ($season=='Second_half'){
$cond = "> '$day'";
}
if ($season=='All_season'){
$cond = "<>''";
}
$tempdate = date("m/d/Y");
$cond2 = "< '$tempdate'";
//-----1--list of teams
$i=0;
$qs = mysql_query("SELECT gdate, hteam FROM games WHERE league='$league' and gtime <> 'CANCELLED' and gdate ".$cond." GROUP BY 'hteam' ");
while($team = mysql_fetch_array($qs))
{
$array_team[$i] = $team['hteam'];
$i++;
}
$qs = mysql_query("SELECT gdate, ateam FROM games WHERE league='$league' and gtime <> 'CANCELLED' and gdate ".$cond." GROUP BY 'ateam' ");
while($team = mysql_fetch_array($qs))
{
$array_team[$i] = $team['ateam'];
$i++;
}
$iterat=0;
$flag=0;
$num=0;
$new='true';
for ($i2=0; $i2<$i; $i2++){
if ($iterat>0){
for($i3=0; $i3<$num; $i3++){
if($array_team_new[$i3]!=$array_team[$i2]){
$flag++;
}
}
if ($flag==$num) {$new='true';}
$flag=0;
}
if ($new=='true'){
$array_team_new[$num] = $array_team[$i2];
$num++;
$new='false';
}
$iterat++;
}
//-----2-statistics
$win_h=0; $win_a=0;
$lose_h=0; $lose_a=0;
$tie_h=0; $tie_a=0;
$a=array();
for ($i2=0; $i2<$num; $i2++){
//-----win_h
$qs = mysql_query("SELECT gdate, hteam, COUNT(hteam) FROM games WHERE league='$league' and gtime <> 'CANCELLED' and hteam='".$array_team_new[$i2]."' and hscore>ascore and gdate ".$cond." GROUP BY 'hteam' ");
while($team_win_h = mysql_fetch_array($qs))
{
$win_h = $team_win_h['COUNT(hteam)'];
}
//-----win_a
$qs = mysql_query("SELECT gdate, ateam, COUNT(ateam) FROM games WHERE league='$league' and gtime <> 'CANCELLED' and ateam='".$array_team_new[$i2]."' and hscore<ascore and gdate ".$cond." GROUP BY 'ateam' ");
while($team_win_a = mysql_fetch_array($qs))
{
$win_a = $team_win_a['COUNT(ateam)'];
}
//-----lose_h
$qs = mysql_query("SELECT gdate, hteam, COUNT(hteam) FROM games WHERE league='$league' and gtime <> 'CANCELLED' and hteam='".$array_team_new[$i2]."' and hscore<ascore and gdate ".$cond." GROUP BY 'hteam' ");
while($team_lose_h = mysql_fetch_array($qs))
{
$lose_h = $team_lose_h['COUNT(hteam)'];
}
//-----lose_a
$qs = mysql_query("SELECT gdate, ateam, COUNT(ateam) FROM games WHERE league='$league' and gtime <> 'CANCELLED' and ateam='".$array_team_new[$i2]."' and hscore>ascore and gdate ".$cond." Group BY 'ateam' ");
while($team_lose_a = mysql_fetch_array($qs))
{
$lose_a = $team_lose_a['COUNT(ateam)'];
}
//-----tie_h
$qs = mysql_query("SELECT gdate, hteam, COUNT(hteam) FROM games WHERE league='$league' and hscore < '99' and gtime <> 'CANCELLED' and hteam='".$array_team_new[$i2]."' and hscore=ascore and gdate ".$cond2." GROUP BY 'hteam' ");
while($team_tie_h = mysql_fetch_array($qs))
{
$tie_h = $team_tie_h['COUNT(hteam)'];
}
//-----tie_a
$qs = mysql_query("SELECT gdate, ateam, COUNT(ateam) FROM games WHERE league='$league' and hscore < '99' and gtime <> 'CANCELLED' and ateam='".$array_team_new[$i2]."' and hscore=ascore and gdate ".$cond2." GROUP BY 'ateam' ");
while($team_tie_a = mysql_fetch_array($qs))
{
$tie_a = $team_tie_a['COUNT(ateam)'];
}
$a[$i2][1] = $win_h+$win_a;
$a[$i2][2] = $lose_h+$lose_a;
$a[$i2][3] = $array_team_new[$i2];
$a[$i2][4] = $tie_h+$tie_a;
$a[$i2][5] = $a[$i2][1] + $a[$i2][2] + $a[$i2][4];
if($a[$i2][1] > 0) {
$a[$i2][0] = $a[$i2][1] / $a[$i2][5];
}
if($a[$i2][0] == 0) {
$a[$i2][0] = '.000';
} else if($a[$i2][0] == 1) {
$a[$i2][0] = '1.00';
} else {
$a[$i2][0] = substr($a[$i2][0],0,5);
}
$win_h=0;
$win_a=0;
$lose_h=0;
$lose_a=0;
$tie_h=0;
$tie_a=0;
}
function cmp($a, $b){
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
//output
$findme = '(N)';
usort($a, "cmp");
echo "<table width='464' border='1'><tr><td width='215'><font face='Century'>Team</font></td>";
echo "<td width='65'><div align='center'><font face='Century'>Win</font></div></td>";
echo "<td width='71'><div align='center'><font face='Century'>Loss</font></div></td>";
echo "<td width='85'><div align='center'><font face='Century'>Tie</font></div></td></tr>";
while (list($key, $value) = each($a)) {
$pos = strpos($value[3],$findme);
$pos2 = strpos($value[3],$findme);
if($pos == false && $pos2 == false) {
echo "<TR>";
echo "<TD>$value[3] ($value[0])</TD><TD> $value[1] </TD><TD>$value[2]</TD><TD>$value[4] </TD>" ;
echo "</TR>";
}
}
echo "</TABLE>";
}
?>feyd | Please review how to post code using
Code: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]