Categories: Development

Simple AFL ladder with PHP

Here is how to use an open API to display the AFL Ladder with PHP.

The concept is simple; Call the ladder api, start a table and then in a foreach loop go through the results.

The foreach loop represents a row for each loop, this is the same as building a table from MySQL data except this is with JSON formatted data.

As always dont abuse any open API’s and cache them in a database where possible.

PHP code is:



#
Team
Points
Wins
Lost
Played
Percent
Points for
Points against
Last 5
vs next
";//Start table and have headers outside of the loop
foreach ($data['positions'] as $val) {//loop
    echo "";//start row
    $team = $val['teamName']['teamName'];
    $position = $val['thisSeasonRecord']['ladderPosition'];
    $points = $val['thisSeasonRecord']['aggregatePoints'];
    $percent = $val['thisSeasonRecord']['percentage'];
    $next_vs = $val['nextOpponent']['teamName'];
    $points_for = $val['pointsFor'];
    $points_against = $val['pointsAgainst'];
    $last_5_wins = $val['lastFiveGamesRecord']['wins'];
    $last_5_draws = $val['lastFiveGamesRecord']['draws'];
    $last_5_losses = $val['lastFiveGamesRecord']['losses'];
    $wins = $val['thisSeasonRecord']['winLossRecord']['wins'];
    $losses = $val['thisSeasonRecord']['winLossRecord']['losses'];
    $played = $val['thisSeasonRecord']['winLossRecord']['played'];
    if ($position > 8) {
        $row_color = '#de7c8d';//if position greater than 8 (not in finals position)
    } else {
        $row_color = '#94dea3';
    }
    echo "$position";
    echo "$team";
    echo "$points";
    echo "$wins";
    echo "$losses";
    echo "$played";
    echo "$percent";
    echo "$points_for";
    echo "$points_against";
    echo "W:$last_5_wins L:$last_5_losses D:$last_5_draws";
    echo "$next_vs";
    echo "";//end row
}
echo "";//close table

Some minimal styling (add above

Gives you this

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago