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:
<?php
$data = json_decode(file_get_contents('https://www.afl.com.au/aflrender/get?service=ladder&site=AFL¶ms=seasonId?:CD_S2019014,roundId?:CD_R201901402&field=json&competitionType=AFL'), true);//The ladder api call
echo "<table>
<tr>
<th>#</th>
<th>Team</th>
<th>Points</th>
<th>Wins</th>
<th>Lost</th>
<th>Played</th>
<th>Percent</th>
<th>Points for</th>
<th>Points against</th>
<th>Last 5</th>
<th>vs next</th>
</tr>";//Start table and have headers outside of the loop
foreach ($data['positions'] as $val) {//loop
echo "<tr>";//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 "<td style='background: $row_color'>$position</td>";
echo "<td style='background: $row_color'>$team</td>";
echo "<td style='background: $row_color'>$points</td>";
echo "<td style='background: $row_color'>$wins</td>";
echo "<td style='background: $row_color'>$losses</td>";
echo "<td style='background: $row_color'>$played</td>";
echo "<td style='background: $row_color'>$percent</td>";
echo "<td style='background: $row_color'>$points_for</td>";
echo "<td style='background: $row_color'>$points_against</td>";
echo "<td style='background: $row_color'>W:$last_5_wins L:$last_5_losses D:$last_5_draws</td>";
echo "<td style='background: $row_color'>$next_vs</td>";
echo "</tr>";//end row
}
echo "</table>";//close tableSome minimal styling (add above <?php):
<style>
body {
background: #87b0ed;
color: #3d4042;
}
th {
background-color: #82a6de;
color: white;
}
table {
border-collapse: collapse;
}
th {
text-align: center;
}
th, td {
padding: 10px;
}
table, th, td {
border: 1px solid black;
}
tr:hover {
background-color: #f5f5f5;
color: white;
}
</style>Gives you this
