Twig foreach group by date

寵の児 提交于 2019-12-24 13:00:25

问题


I have a table with football matches called 'matches'. In that table there is a field 'kickoff', which is a datetime-column of the time when the match starts. With the following code I get all the matches out of the table.

$matches = $em->createQueryBuilder()
        ->select('m')
        ->from('FootballWcBundle:Matches', 'm')
        ->addOrderBy('m.kickoff', 'ASC')
        ->getQuery()
        ->getResult();

return $this->render('FootballWcBundle:Page:matches.html.twig', array(
        'matches' => $matches
));

Now I want to show the matches on the screen grouped by the date. Like this:

12-12-2014

match1

match2

14-12-2014

match3

match4

match5

Is there a way to let Twig know to group by the kickoff column or is there another way to do this?


回答1:


You could do that as below:

    {% set date = null %}
    {% for match in matches %}
        {% if date != match.kickoff %}
            {% set date = match.kickoff %}
            <h3>{{ date }}</h3>
        {% endif %}

        <p>{{ match.name }}</p>
    {% endfor %}

In this way, you set the first date as null, and you iterate all matches and write a 'p' tag with the name (I supposed the match had a name to do the example), and when the date of the matches changes, you write a 'h3' tag with the date of the match. As date is set to null in the first iteration, the first date will be write.




回答2:


There is a controlled break algorithm that originated from COBOL. It can be used in your case, you would have to rewrite it in TWIG though. I successfully used it in JAVA.

Below is C/C++ implementation of the controlled break algorithm. Take a look and use this principle for your case.

#include <iostream>
#define LMARG "          "
#define UNDERLN "=============="

using namespace std;

void displayTable(int table[],short size);
//
// Purpose: Illustrated a controlled break algorithm
// Given an ordered sequence of data members, display with summary
//
int main(void)
{
    int table[] = {10,12,12,30,30,30,30,40,55,60,60,60};

    displayTable(table,sizeof(table)/sizeof(int));
    fflush(stdin);
    getchar();
    return 0;
}


void displayTable(int table[],short size)
{
    int currentKey,lastKey;
    int i, categoryCount,groupTotal;

    currentKey = lastKey = table[0];
    groupTotal = categoryCount = i = 0;
    while (i < size)
    {
        if (currentKey == lastKey)
        {
            groupTotal += table[i];
            categoryCount++;
            cout << LMARG <<  table[i] << endl;
        }
        else
        {
            cout << LMARG <<  UNDERLN << endl;
            cout << "Total:       " << groupTotal << endl;
            cout << "Croup Count: " <<  categoryCount << endl << endl;
            cout << LMARG <<  table[i] << endl; // start of next group
            groupTotal = currentKey,categoryCount = 1;
            fflush(stdin);getchar();
        }
        lastKey = currentKey;
        currentKey = table[++i]; // get next key
    } // while
    cout << LMARG <<  UNDERLN << endl; // last group summary
    cout << "Total:       " << groupTotal << endl;
    cout << "Croup Count: " <<  categoryCount << endl << endl;
}


来源:https://stackoverflow.com/questions/23896701/twig-foreach-group-by-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!