1

I am building a baseball standings table. I currently have a Single section setup in Craft to handle some basic info. It contains a table field with 3 rows: Team, Wins and Losses.

I am able to output this info via the following code:

{% for records in standings %}
    {% for row in records.southStandings %}
        <tr>
            <td>{{ row.team }}</td>
            <td>{{ row.wins }}</td>
            <td>{{ row.losses }}</td>
            <td></td>
            <td>{{ row.wins + row.losses }}</td>
            <td>{{ (row.wins / (row.wins + row.losses))|round(3, 'common') }}</td>
            <td></td>
        </tr>
    {% endfor %}
{% endfor %}

standings table

However I am needing to do a few things for these standings.

  1. Needing to sort the teams via their winning percentage (W%), if W% is same then alphabetically.

The W% is calculated this way: {% set wP = (team.wins / (team.wins + team.losses))|round(3, 'common') %}

  1. Calculate the games behind (GB).

Games behind is another equation, like so: {% set gamesBackA = ((teamA.wins - teamB.wins) + (teamB.losses - teamA.losses)) / 2 %}

GB is how far back teams are from the leading team (1st place).

I am not even sure how I would set this and output the correct number at all, so any guidance on this would be wonderful.

I have tried setting an new empty array before my for loop and setting it within the loop. But the only way it outputs more than won W% number in that array is another for loop, like so:

{% set standings = craft.entries.section('standings') %}
{% set wPArray = [] %}

{% for teams in standings %}
    {% for team in teams.southStandings %}
        {% set wP = (team.wins / (team.wins + team.losses))|round(3, 'common') %}
        {% set wPArray = wPArray|merge([wP]) %}
    {% endfor %}
{% endfor %}

{% for t in wPArray %}
    {{ t }}
{% endfor %}

I can then sort using SuperSort but that doesn't allow me to output the Team, Wins, Losses, Games Played (GP), and Games Back.

What am I missing with the arrays?

What is the best approach to output the table, sort them by W%/alphabetically and calculate Games Back as well.

I do have the SuperSort plugin installed.

Any help with this would be greatly appreciated.

Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.