Reputation:
Trying to loop through my query results and pass them into twig to print them, however it is printing them each at a time. Is there a better way to loop through the results with twig?
I've tried different combinations of twig loops but it loops once and prints each results per <li>
tag.
Here is my Twig code.
<ul>
{% for status in status %}
<li>{{ status }}</li>
{% endfor %}
{% for invoiceNumber in invoiceNumber %}
<li>{{ invoiceNumber }}</li>
{% endfor %}
{% for createdAt in createdAt %}
<li>{{ createdAt }}</li>
{% endfor %}
{% for amount in amount %}
<li>{{ amount }}</li>
{% endfor %}
{% for sourceCode in sourceCode %}
<li>{{ sourceCode }}</li>
{% endfor %}
{% for fundCode in fundCode %}
<li>{{ fundCode }}</li>
{% endfor %}
{% for keyword in keyword %}
<li>{{ keyword }}</li>
{% endfor %}
</ul>
Here is what it prints
<li>2
<li>2
<li>000035772641
<li>000035772861
<li>03/26/19
<li>03/26/19
<li>20
<li>80
<li>G19W2KACTB
<li>K100
<li>vvK100
What I would like for it to print is
per <ul>
tag
<li>2
<li>000035772641
<li>03/26/19
<li>20
<li>G19W2KACTB
<li>vvK100
<li>2
<li>000035772861
<li>03/26/19
<li>80
<li>
<li>K100
Id like to print each results withing 1 <ul>
tag through a loop. Is anyone familiar with twig that might spot the problem and help me with a solution?
Upvotes: 0
Views: 357
Reputation: 11
I think the best way would be to place all your values into an array inside your controller.
I don't know what your controller looks like, but you would need an array looking like this.
$results = array(
array(
'status' => "something",
'invoiceNumber' => "123",
'createdAt' => "2019-03-29",
'amount' => 2.00,
'sourceCode' => "<div>blah blah</div>",
'fundCode' => "Something",
'keyword' => "Something else"
),
array(
'status' => "something",
'invoiceNumber' => "123",
'createdAt' => "2019-03-29",
'amount' => 2.00,
'sourceCode' => "<div>blah blah</div>",
'fundCode' => "Something",
'keyword' => "Something else"
)
.....
);
Essentially then you could loop your results like so:
{% for result in results %}
<ul>
<li>{{ result.status }}</li>
<li>{{ result.invoiceNumber }}</li>
<li>{{ result.createdAt }}</li>
<li>{{ result.amount }}</li>
<li>{{ result.sourceCode }}</li>
<li>{{ result.fundCode }}</li>
<li>{{ result.keyword }}</li>
</ul>
{% endfor %}
That should do the trick.
Upvotes: 1
Reputation: 58
Show your full controller first.
Anyway, better and shorter way to loop over your query result is create something like this in Twig
{% for result in results %}
<ul>
<li>{{ result.invoiceNumber }}</li>
<li>{{ result.createdAt }}</li>
<li>{{ result.amount }}</li>
.......
</ul>
{% endfor %}
Where result is single row in results array of fetched rows from DB
Upvotes: 2