1
{exp:channel:entries channel="ranking" order="points" sort="desc"}
{team} | {points} | {goals_scored} | {goals_against} 
{/exp:channel:entries} 

The code works fine,but when there are two teams locked at same points I want the Goal difference to take over the responsibilty of ranking teams.I do have the goal_scored and goal_against field.

How do I do this?

3
  • By Goal difference, do you mean {goals_scored}-{goals_against}? – Sobral Jun 2 '14 at 15:04
  • @RobsonSobral : yes,exactly. – user2133 Jun 2 '14 at 18:32
  • Check the new answer, please. – Sobral Jun 2 '14 at 18:57
3

If by "Goal difference" you mean {goals_scored} - {goals_against}, you gonna have to write a custom query.

With the Channel Module, the closer you can get is to sort both fields in different directions:

{exp:channel:entries channel="ranking" orderby="points|goals_scored|goals_against" sort="desc|desc|asc"}
{team} | {points} | {goals_scored} | {goals_against}
{/exp:channel:entries}

Or, for a perfect result, code your own query:

{exp:query
sql="
SELECT
    ct.title AS event_title, 
    ct.url_title AS event_url_title,
    ct.entry_id AS entry_id,
    cd.field_id_ID1 AS team,
    cd.field_id_ID2 AS points,
    cd.field_id_ID3 AS goals_scored,
    cd.field_id_ID4 AS goals_against,
    cd.field_id_ID3 - cd.field_id_ID4 AS goal_difference
FROM exp_channel_titles AS ct
JOIN exp_channel_data AS cd ON ct.entry_id = cd.entry_id
WHERE ct.channel_id = 2
AND ct.expiration_date NOT BETWEEN 1 AND unix_timestamp(Now())
AND ct.entry_date <= unix_timestamp(Now())
ORDER BY points DESC, difference DESC
"}
        {team} | {points} | {goals_scored} | {goals_against}
{/exp:query}

I added some fields that could be useful and prevented to show expired or future entries. You still need to change the IDs of the fields.

2
  • Exactly.but will this give me perfect results? – user2133 Jun 2 '14 at 17:26
  • @Vagabond, this is a different logic, but, for your question, "Order considering two fields", this is the right answer. For what you really need, "order by the subtraction of two fields", you have to code a custom query. An easy query, actually. – Sobral Jun 2 '14 at 18:03
1

Another option if available in your build would be to use Stash. You could simply build a list containing the following for each ranking

  • points
  • team
  • goals_scored
  • goals_against
  • goals_difference (which you could calculate using something like mx calculator

You could then use multiple orders and sorts (amongst which the goals difference) when using {exp:stash:get_list}

Not tested but something along the lines of:

Build list

{exp:stash:set_list name="st_rankings" parse_tags="yes"}
    {exp:channel:entries channel="ranking" disable="categories|category_fields|member_data|pagination" dynamic="no"}
        {stash:item_team}{team}{/stash:item_team}
        {stash:item_points}{points}{/stash:item_points}
        {stash:item_goals_scored}{goals_scored}{/stash:item_goals_scored}
        {stash:item_goals_against}{goals_against}{/stash:item_goals_against}
        {stash:item_goals_difference}{exp:mx_calc expression="{goals_scored} - {goals_against}"}{/stash:item_goals_difference}
    {/exp:channel:entries}
{/exp:stash:set_list}

Output list

{exp:stash:get_list name="st_rankings" orderby="item_points|item_goals_difference" sort="desc|desc" sort_type="numeric|numeric"}
    <h2>Team: {item_team}</h2>
    <p>Points: {item_points}</p>
    <p>Goals scored: {item_goals_scored}</p>
    <p>Goals against: {item_goals_against}</p>
    <p>Goals difference: {item_goals_difference}</p>
{/exp:stash:get_list}
3
  • just went through stash and what it can do..I posted my answer.Still,i ll be greatful if you can show me a bettr way – user2133 Jun 4 '14 at 13:40
  • Added some code to get you started (although Robson gave you a working solution already). – Jérôme Coupé Jun 4 '14 at 20:47
  • Wow.Thx for the response. – user2133 Jun 5 '14 at 4:52

Your Answer

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