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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago