I have a table with 10 rows and 10 columns with int values; I need the total to be print at 11th row, also i need to total and print at columns.
Example :
cell(1x11) = row1+row2+row3...row10;
cell(2x11) = row1+row2+row3...row10;
and
cell(11x1) = col1+col2+col3...col10;
cell(11x2) = col1+col2+col3...col10;
How can i do this easily.
Required result :
<table>
<tr>
<th></th>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Total</th>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<th>Total</th>
<td>3</td>
<td>6</td>
<td>9</td>
<td>18</td>
</tr>
</table>
My actual twig code:
`
{% set len = entities|length %}
{% set len_e = exams|length %}
{% if len_e != null and len_e is not empty and len_e > 0 %}
<div class="table-responsive">
<table id="FZUserExam" class="table table-striped table-bordered">
<thead>
<tr>
<th rowspan="2">SUBJECT</th>
<th rowspan="2">Max<br/>Marks</th>
{% for i in 1..len_e %}
{% if i == 1 %}
{% set terms = exams[0].term %}
{% set x = 1 %}
{% else %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% else %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% endif %}
{% if i == len_e %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% endif %}
{% endfor %}
<th rowspan="2">Grand<br/>Total</th>
</tr>
<tr>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
<th>{{ exams[i-1].exam }}</th>
{% else %}
<th>Total</th>
<th>{{ exams[i-1].exam }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<th>Total</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% set y = 0 %}
{% set total = 0 %}{% set gtotal = 0 %}
{% for a,s in subject %}
<tr>
<td>{{ s.name }}</td>
<td>{{ s.maxMarks }}</td>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
<td>{{ gtotal }}{% set gtotal = 0 %}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
<tr class="totalColumn">
<td>Total</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>
{#<tr class="totalGrade">
<td>Grade</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>#}
</tbody>
</table>
</div>
{% else %}
<h3 class="text-center">Exam Table Not Updated</h3>
{% endif %}
</div>`
I don't know what data structure you're using to memorize your ints, but you could start with something like this:
<table>
{% for r in rows %}
<tr>
{% set rowTotal = 0 %}
{% for c in r.columns %}
{% set rowTotal = rowTotal + c.value %}
<td>{{ c.value }}</td>
{% endfor %}
<td>{{ rowTotal }}</td>
</tr>
{% endfor %}
</table>
Related
I have all these {% for %} loops that are nested one inside the other but I would like them to be all at the same level. Independent from one another because like this, my goalkeepers only appear if there are defenders, midfielders... I would like them all to appear wether one category is missing or not.
<tbody>
{% for defenser in defensers %}
{% for midfielder in midfielders %}
{% for forward in forwards %}
{% for gk in goalkeepers %}
<tr>
<td>
<div class="player-in-club">
{{ gk.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ defenser.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ midfielder.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ forward.name }}
</div>
</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
</tbody>
You can ensure that your arrays always contain at least one (empty string) Element if you put this in front of your for loops:
{% if defensers is empty %}
{% set defensers = [""] %}
{% endif %}
{% if midfielders is empty %}
{% set midfielders = [""] %}
{% endif %}
{% if forwards is empty %}
{% set forwards = [""] %}
{% endif %}
{% if goalkeepers is empty %}
{% set goalkeepers = [""] %}
{% endif %}
But I feel like you want something kompletely differently. The way you have set up your loops they will print every possible row kombination.
If you just want a table that is filled as the items are available you might want to use this instead of your loops:
{% set row_count = max(defensers|length, midfielders|length, forwards|length, goalkeepers|length) %}
<tbody>
{% for index in range(0, row_count - 1) %}
<tr>
<td>
<div class="player-in-club">
{% if goalkeepers[index] %}
{{ goalkeepers[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if defensers[index] %}
{{ defensers[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if midfielders[index] %}
{{ midfielders[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if forwards[index] %}
{{ forwards[index].name }}
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</tbody>
I tried to upgrade Symfony to 4.4.17 and I got the above error for the line:
{% for lang, group in lang_groups if lang == from %}
This is the code. I have no idea how to fix this.
<div class="input-style">
<input type="text" name="query" value="{{ query|default('') }}" />
</div>
<div class="select-style">
<select name="lang_from">
{% for lang, group in lang_groups %}
<option value="{{ lang }}"{% if from == lang %} selected{% endif %}>{{ group.label }}</option>
{% endfor %}
</select>
</div>
⇄
{% for lang, group in lang_groups if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endfor %}
You have to put the if statement out of your for...in
So instead of this
{% for lang, group in lang_groups if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endfor %}
If I understood what you are trying to do, you should do something like this:
{% for lang, group in lang_groups %}
{% if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endif %}
{% endfor %}
How I can grouping item in list, if I have different items with one ID?
I want if loop find two or more items with the same id combine them in to list ul > li.
For example:
{% extends 'base.html.twig' %}
{% block wrapper %}
<div class="panel panel-default">
<div class="panel-heading clearfix add">
Заявки
</div>
<table class="table">
<tr>
<th>ID</th>
<th>ID заявки</th>
<th>Склад</th>
<th>Матеріал</th>
<th>Кількість</th>
<th>Сумма</th>
<th>Дата</th>
<th>Час</th>
<th>Користувач</th>
</tr>
{% if itemEntries == null %}
<tr>
<td colspan="6">Елементи для показу відсутні</td>
</tr>
{% endif %}
{% for value in itemEntries %}
<tr>
<td>{{ value.id }}.</td>
<td>{{ value.bid }}</td>
<td>{{ value.storage.name }}</td>
<td>{{ value.item.name }} (ID: {{ value.item }})</td>
<td>{{ value.count }}</td>
<td>-{{ value.count * value.item.price }}</td>
<td>{{ value.date | date('d-m-Y') }}</td>
<td>{{ value.time | date('H:m:i') }}</td>
<td>{{ value.userLogin }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
Thanks!
The best way is to group the items in a SQL query.
So in you controller you should using groupBy() to grouping the result.
Look at this example:
$itemEntries = $this->em->createQueryBuilder()
->select('e')
->from('AppBundle:Entry', 'e')
->groupBy('e.bidId')
->getQuery()
->getResult();
{% for item in product %}
<tr>
<td class="col-lg-5" > <h5> {{ item.tensp}}</h5></td>
<td class="col-lg-12"> {{ item.thanhphan}}</td>
<td class="col-lg-12"> {{ item.gia}}</td>
line 19 <td class="col-lg-12"> <img src="{{asset('/bundles/image/'{item.hinh})}}" alt=""/></td>
</tr>
{% endfor %}
I got this error message in Symfony Arguments must be separated by a comma. Unexpected token "punctuation" of value "{" ("punctuation" expected with value ",") in SP\findall.html.twig at line 19.
how can i fix it
Try concat string with ~ operator.
{% for item in product %}
<tr>
<td class="col-lg-5" ><h5> {{ item.tensp}}</h5></td>
<td class="col-lg-12">{{ item.thanhphan}}</td>
<td class="col-lg-12">{{ item.gia }}</td>
<td class="col-lg-12"><img src="{{ asset('/bundles/image/' ~ item.hinh)}}" alt=""/></td>
</tr>
{% endfor %}
{% for tableField in tableFieldsArr %}
<tr>
<td>
<select>
<option name="{{ tableField }}">{{ tableField }}</option>
</select>
</td>
</tr>
{% endfor %}
I am trying to generate a select box with its options for each tr row with the above code I am getting a select box with 1 item in each row. How can I fix it?
$tablefieldsArr comes from the controller.
You have to move the for loop inside your select:
{% for i in 0..tableFieldsArr|length %}
<tr>
<td>
<select>
{% for tableField in tableFieldsArr %}
<option name="{{ tableField }}">{{ tableField }}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}