I need help in optimizing this code.
I'd like to create the initial arrays from a query that extracts all
teams with the Division 'A'. Then I'd like to be able to loop through
the DB results below and accumulate the wins, losses, ties and
points for each team.
Don't get me wrong, the code below works, I'd just like to shorten
it up and not have to modify it every time I add/subtract a team
and to be able to make one function to build standings for each
of my four divisions.
Any tips on how to make this code run "better, stronger, faster"?
<?
function austinstandings() {
include("board/db/mysql.php");
include("../dbconnect.inc");
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$a = array(
'bandits' =>
array(
'wins' => 0,
'losses' => 0,
'ties' => 0,
'points' => 0,
),
'devil rays' =>
array(
'wins' => 0,
'losses' => 0,
'ties' => 0,
'points' => 0,
)
);
// select only Austin Division
$sql = 'SELECT g.* FROM games g, teams t1, teams t2 ';
$sql = $sql.'WHERE (g.homeid=t1.id AND g.visitorid=t2.id)';
$sql = $sql.' AND (t1.division="A" or t2.division="A")';
$sql = $sql.' AND (g.homescore IS NOT NULL AND g.visitorscore IS NOT NULL)';
$sql = $sql.' AND (g.homescore<>999 AND g.visitorscore<>999)'
$sql = $sql.' AND (g.homescore<>777 AND g.visitorscore<>777)';
$sql = $sql.'ORDER BY g.gamenum';
$query = $db->query($sql);
// compute the standings
for ($i=0; $i< $db->num_rows($query); $i++) {
$row = $db->fetch_row($query);
// 999 is for Rainouts and 777 is for Cancelled games
if (($row[5] <> 999 || $row[5] <> 777) and ($row[6] <> 999 || $row[6] <> 777))
{
switch ($row[3]) { // Visitor
case "37": // Bandits
if ($row[5] > $row[6]) {
$a['bandits']['wins']++; // Win
$a['bandits']['points']++; // 3 points for win
$a['bandits']['points']++;
$a['bandits']['points']++;
} elseif ($row[5] == $row[6]) {
$a['bandits']['ties']++; // Tie
$a['bandits']['points']++; // 1 point for tie
} else
$a['bandits']['losses']++; // Loss, no points
break;
case "14": // devil rays
if ($row[5] > $row[6]) {
$a['devil rays']['wins']++; // Win
$a['devil rays']['points']++; // 3 points for win
$a['devil rays']['points']++;
$a['devil rays']['points']++;
} elseif ($row[5] == $row[6]) {
$a['devil rays']['ties']++; // Tie
$a['devil rays']['points']++; //1 point for tie
} else
$a['devil rays']['losses']++; // Loss, no points
break;
} // switch ($row[5])
} // if (($row[5] <> 999) and ($row[6] <> 999))
if (($row[5] <> 999 || $row[5] <> 777) and ($row[6] <> 999 || $row[6] <> 777))
{
switch ($row[4]) { // Home
case "37"; // Bandits
if ($row[6] > $row[5]) {
$a['bandits']['wins']++; // Win
$a['bandits']['points']++; // 3 points for win
$a['bandits']['points']++;
$a['bandits']['points']++;
} elseif ($row[6] == $row[5]) {
$a['bandits']['ties']++; // Tie
$a['bandits']['points']++; // 3 points for win
} else
$a['bandits']['losses']++; // Loss, no points
break;
case "14": // devil rays
if ($row[6] > $row[5]) {
$a['devil rays']['wins']++; // Win
$a['devil rays']['points']++; // 3 points for win
$a['devil rays']['points']++;
$a['devil rays']['points']++;
} elseif ($row[6] == $row[5]) {
$a['devil rays']['ties']++; // Tie
$a['devil rays']['points']++; // 3 points for win
} else
$a['devil rays']['losses']++; // Loss, no points
break;
} // switch ($row[6])
} // if (($row[5] <> 999) and ($row[6] <> 999))
} // for ($i=0; $i< $db->num_rows($query); $i++)
return $a;
}
?>