Sort.

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
rmurdo
Forum Newbie
Posts: 5
Joined: Wed Mar 03, 2004 2:03 am

Sort.

Post by rmurdo »

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:

Code: Select all

teama 2 wins 0 loss and 0 ties
teamb 2 wins 1 loss and 0 ties
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!

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

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the logic in your code is hard to follow.. :?
rmurdo
Forum Newbie
Posts: 5
Joined: Wed Mar 03, 2004 2:03 am

Code.

Post by rmurdo »

I agree.. I paid somone for that, belive it or not.
php I ninja
Forum Newbie
Posts: 1
Joined: Wed Apr 20, 2005 11:42 am

Post by php I ninja »

The code above is crap. Whats your db schema? ie. Table structure for 'games' ? and if you could provide a brief description for each field that would be awesome.

Regards
-m
rmurdo
Forum Newbie
Posts: 5
Joined: Wed Mar 03, 2004 2:03 am

Schema.

Post by rmurdo »

php I ninja wrote:The code above is crap. Whats your db schema? ie. Table structure for 'games' ? and if you could provide a brief description for each field that would be awesome.

Regards
-m
`id` int(8) NOT NULL auto_increment,
`gdate` varchar(40) NOT NULL default '0000-00-00',
`gday` varchar(11) NOT NULL default '',
`gtime` varchar(11) NOT NULL default '',
`hteam` varchar(60) NOT NULL default '',
`hscore` char(2) default '99',
`ateam` varchar(60) NOT NULL default '',
`ascore` char(2) default '99',
`field` varchar(60) NOT NULL default '',
`league` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)

its id, game date, day of week, time of game, home team name, home team score, away team name, away team score, field and their league.

Rob
Post Reply