';
$q ="SELECT t1.teamID, t1.teamName, t1.division,
(IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins)) AS 'wins',
(IF(t2.hLoss IS NULL, 0, t2.hLoss) + IF(t3.aLoss IS NULL, 0, t3.aLoss)) AS 'loss',
(IF(t2.hTie IS NULL, 0, t2.hTie) + IF(t3.aTie IS NULL, 0, t3.aTie)) AS 'tie',
(IF(t2.hGfor IS NULL, 0, t2.hGfor) + IF(t3.aGfor IS NULL, 0, t3.aGfor)) AS 'ptsfor',
(IF(t2.hGagainst IS NULL, 0, t2.hGagainst) + IF(t3.aGagainst IS NULL, 0, t3.aGagainst)) AS 'ptsagainst',
((IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins)) /((IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins))+(IF(t2.hLoss IS NULL, 0, t2.hLoss) + IF(t3.aLoss IS NULL, 0, t3.aLoss))) ) AS 'winpct'
FROM teams AS t1
LEFT JOIN (SELECT homeID, SUM(homepts) AS 'hGfor', SUM(awaypts) AS 'hGagainst', SUM(IF(homepts > awaypts,1,0)) AS 'hWins', SUM(IF(homepts < awaypts,1,0)) AS 'hLoss',SUM(IF(homepts = awaypts,1,0)) AS 'hTie' FROM schedule WHERE seasonID=".$SEASON." AND leagueID=".$LEAGUE." AND gameType='Season' AND gameEnd <> '' GROUP BY homeID) t2 ON t1.teamID=t2.homeID
LEFT JOIN (SELECT awayID, SUM(awaypts) AS 'aGfor', SUM(homepts) AS 'aGagainst', SUM(IF(awaypts > homepts,1,0)) AS 'aWins', SUM(IF(awaypts < homepts,1,0)) AS 'aLoss',SUM(IF(awaypts = homepts,1,0)) AS 'aTie' FROM schedule WHERE seasonID=".$SEASON." AND leagueID=".$LEAGUE." AND gameEnd <> '' GROUP BY awayID) t3 ON t1.teamID=t3.awayID
WHERE t1.seasonID=".$SEASON." AND t1.leagueID=".$LEAGUE." AND t1.division='Community'
GROUP BY t1.teamID
ORDER BY t1.division,wins DESC, tie DESC, winpct DESC, t1.teamName";
$div1 = $db->getdata_safe($q);
$q ="SELECT t1.teamID, t1.teamName, t1.division,
(IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins)) AS 'wins',
(IF(t2.hLoss IS NULL, 0, t2.hLoss) + IF(t3.aLoss IS NULL, 0, t3.aLoss)) AS 'loss',
(IF(t2.hTie IS NULL, 0, t2.hTie) + IF(t3.aTie IS NULL, 0, t3.aTie)) AS 'tie',
(IF(t2.hGfor IS NULL, 0, t2.hGfor) + IF(t3.aGfor IS NULL, 0, t3.aGfor)) AS 'ptsfor',
(IF(t2.hGagainst IS NULL, 0, t2.hGagainst) + IF(t3.aGagainst IS NULL, 0, t3.aGagainst)) AS 'ptsagainst',
((IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins)) /((IF(t2.hWins IS NULL, 0, t2.hWins) + IF(t3.aWins IS NULL, 0, t3.aWins))+(IF(t2.hLoss IS NULL, 0, t2.hLoss) + IF(t3.aLoss IS NULL, 0, t3.aLoss))) ) AS 'winpct'
FROM teams AS t1
LEFT JOIN (SELECT homeID, SUM(homepts) AS 'hGfor', SUM(awaypts) AS 'hGagainst', SUM(IF(homepts > awaypts,1,0)) AS 'hWins', SUM(IF(homepts < awaypts,1,0)) AS 'hLoss',SUM(IF(homepts = awaypts,1,0)) AS 'hTie' FROM schedule WHERE seasonID=".$SEASON." AND leagueID=".$LEAGUE." AND gameType='Season' AND gameEnd <> '' GROUP BY homeID) t2 ON t1.teamID=t2.homeID
LEFT JOIN (SELECT awayID, SUM(awaypts) AS 'aGfor', SUM(homepts) AS 'aGagainst', SUM(IF(awaypts > homepts,1,0)) AS 'aWins', SUM(IF(awaypts < homepts,1,0)) AS 'aLoss',SUM(IF(awaypts = homepts,1,0)) AS 'aTie' FROM schedule WHERE seasonID=".$SEASON." AND leagueID=".$LEAGUE." AND gameType='Season' AND gameEnd <> '' GROUP BY awayID) t3 ON t1.teamID=t3.awayID
WHERE t1.seasonID=".$SEASON." AND t1.leagueID=".$LEAGUE." AND t1.division='Provincial'
GROUP BY t1.teamID
ORDER BY t1.division,wins DESC, winpct DESC, tie DESC, t1.teamName";
$div2 = $db->getdata_safe($q);
if(count($div1) == 0 && count($div2) == 0)
echo '
';
$q = "SELECT gameID, date, time, venue, homeID, homepts, h.teamName AS 'homeName', awayID, awaypts, a.teamName AS 'awayName', gameType, modified, gameEnd, h.division "
. "FROM ". TBL_SCHED ." AS s "
. "LEFT JOIN ". TBL_TEAMS ." AS h ON s.homeID = h.teamID "
. "LEFT JOIN ". TBL_TEAMS ." AS a ON s.awayID = a.teamID "
. "WHERE date >='$startdate' AND date <='$enddate' AND s.seasonID=" . $SEASON ." AND s.leagueID=".$LEAGUE." ORDER BY date, time";
$events = $db->getdata_safe($q);
$events_so_far = 0;
$events_total = count($events);
if($fstWday == 1)
$currday = mktime(0,0,0,$month,1,$year);
else
$currday = mktime(0,0,0,$prevmonth,$prefstday,$prevyear); //figure out the first day to display
echo "