I just tried out this code below and it doesn't seem to work. I corrected the bugs and it doesn't show nothing. It should show the standings array which I can then make it into a table. It's for Joomla MVC component that I'm working on. Can maybe somebody help me?
Thanks!
The code: EDIT:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('home' , 'scoreHome' , 'away' , 'scoreAway')));
$query->from($db->quoteName('futliga_pc_liga1'));
$db->setQuery($query);
$rows = $db->loadAssocList();
$standings = array ();
$standingTemplate = array ('matches' => 0, 'wins' => 0, 'draws' => 0, 'losses' => 0, 'scoreHome' => 0, 'scoreAway' => 0, 'goalsdiff' => 0, 'points' => 0);
foreach ($rows as $row) {
handleMatch($row['home'], $row['scoreHome'], $row['scoreAway']);
handleMatch($row['away'], $row['scoreAway'], $row['scoreHome']);
echo '<pre>';
print_r( usort($standings, 'comparePoints' ) );
}
function handleMatch($team, $scoreHome, $scoreAway){
global $standings, $standingTemplate;
if ($scoreHome > $scoreAway)
{
$points = 3;
$win = 1;
$draw = 0;
$loss = 0;
}
elseif ($scoreHome == $scoreAway)
{
$points = 1;
$win = 0;
$draw = 1;
$loss = 0;
}
else
{
$points = 0;
$win = 0;
$draw = 0;
$loss = 1;
}
if ( empty($standings[$team])){
$standing = $standingTemplate;
} else {
$standing = $standings[$team];
$standingTemplate['matches']++;
$standingTemplate['wins'] += $win;
$standingTemplate['draws'] += $draw;
$standingTemplate['losses'] += $loss;
$standingTemplate['scoreHome'] += $scoreHome;
$standingTemplate['scoreAway'] += $scoreAway;
$standingTemplate['goalsdiff'] += $scoreHome - $scoreAway;
$standingTemplate['points'] += $points;
}
$standings[$team] = $standing;
}
function comparePoints($a, $b){
if ($a['points'] == $b['points']) {
if ($a['goalsdiff'] == $b['goalsdiff']) return 0;
return ($a['goalsdiff'] < $b['goalsdiff']) ? 1 : -1 ;
}
return ($a['points'] < $b['points']) ? 1 : -1 ;
}