Get key name of array value in twig for loop - php

I'm trying to have twig to output the key name(column name from mysql data).
What I want to do is basicly: <a class="listquestions" href="#" id="{{ key }}"...
Current codebase:
<table id="listquestions" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
{% for key, answer in answers[0] %}
<th>{{ key }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for key,answer in answers %}
<tr>
<td>{{ answer.a_id }}</td>
<td>
<a class="listquestions" href="#" data-name="a_text" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name">
{{ answer.a_text }}
</a>
</td>
<td>
<a class="listquestions" href="#" data-name="a_attribute_name" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name">
{{ answer.a_attribute_name}}
</a>
</td>
</tr>
{% endfor %}
</tbody>
PHP function var_export($data,true) outputs:
array (
0 =>
array (
'a_id' => '1',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
1 =>
array (
'a_id' => '2',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
2 =>
array (
'a_id' => '3',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
)
I tried adding a TwigExtension that does key($answer.a_text) but key() does not work with twig for-loops.
So what am I missing? I can output the key name inside <thead> as you see but I'd like to do this with the second for-loop.

<table id="listquestions" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
{% for key, answer in answers[0] %}
<th>{{ key }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for key,answer in answers %}
{% for field, value in answer %}
<tr>
{% if field == 'a_id' %}
<td>{{ answer.a_id }}</td>
{% else %}
<td>
<a class="listquestions" href="#" data-name="{{ field }}" data-type="text" data-pk="{{ answer.a_id }}" data-url="path_for('qa.edit')" data-title="enter attribute name">
{{ value }}
</a>
</td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>

Related

Tables side by side

How can I put two arrays side by side using twig ?
This is my input
{% for key, item in CompareArray %}
{% for x in item %}
<tr>
{% if key =='last' %}
<td> last</td>
{% endif %}
{% if key == 'primary' %}
<td> primary</td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}
The desired output
table,td{
border: 1px solid black;
padding: 1px;
}
<table>
<tr>
<td> last.val1 </td>
<td> last.val2 </td>
<td> last.val3 </td>
<td> #### </td>
<td> primary.val1 </td>
<td> primary.val2 </td>
<td> primary.val3 </td>
</tr>
<tr>
<td> ... </td>
<td> ... </td>
<td> ... </td>
<td> ... </td>
<td> ... </td>
<td> ... </td>
<td> ... </td>
</tr>
<table>
Based on the shared information I see a couple of possible solutions.
But first confirm if I understand correctly what you're trying to achieve.
You want to have table where the item information of the last group is displayed first with a separator column and then the item information of the primary group on the left? And as seen in the screen shot of the array the number of items in the group isn't even necessarily the same?
First think to consider would be should we create two different tables one for primary group and one for last and style them through css to look as if they're in they are side by side.
{% for groupKey, items in CompareArray %}
<table>
{% for item in items %}
<tr>
<td>{{ groupKey }}.{{ item.prop1 }}</td>
<td>{{ groupKey }}.{{ item.prop2 }}</td>
<td>{{ groupKey }}.{{ item.prop3 }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
Second solution would be a complicated use of attribute this means that both arrays have to have same keys. Here you'd iterate over the largest group and then add the corresponding value from the other group. I'm using some macros to better separate the logic. The block named myTemplate is the code you'd insert where you wanted the data to be displayed.
{% block myTemplate %}
{% if CompareArray.last|length > CompareArray.primary|length %}
{{ _self.lastIsLonger(CompareArray.last, CompareArray.primary) }}
{% else %}
{{ _self.primaryIsLonger(CompareArray.last, CompareArray.primary) }}
{% endif %}
{% endblock %}
{% macro lastIsLonger(lastGroup, primaryGroup) %}
{% set primaryLenght = primaryGroup|length %}
<table>
{% for k,item in lastGroup %}
<tr>
{{ _self.buildElementData(item) }}
<td> #### </td>
{{ _self.buildElementData(loop.index <= primaryLenght ? attribute(primaryGroup, k) : {}) }}
</tr>
{% endfor %}
</table>
{% endmacro %}
{% macro primaryIsLonger(lastGroup, primaryGroup) %}
{% set lastGroupLength = lastGroup|length %}
<table>
{% for k,item in primaryGroup %}
<tr>
{{ _self.buildElementData(loop.index <= lastGroupLength ? attribute(lastGroup, k) : {}) }}
<td> #### </td>
{{ _self.buildElementData(item) }}
</tr>
{% endfor %}
</table>
{% endmacro %}
{% macro buildElementData(item) %}
<td>{{ item.prop1 ?? '' }}</td>
<td>{{ item.prop2 ?? '' }}</td>
<td>{{ item.prop3 ?? '' }}</td>
{% endmacro %}
If I understand what you're doing I think a third solution I think would be best. That is that from the PHP side you matched which items you wanted to compare so the data you'd insert would look something like this:
[
[
'last' => [
'prop1' => '...',
'prop2' => '...',
'prop3' => '...',
],
'primary' => [
'prop1' => '...',
'prop2' => '...',
'prop3' => '...',
],
], [
'last' => [
'prop1' => '...',
'prop2' => '...',
'prop3' => '...',
],
'primary' => [
'prop1' => '...',
'prop2' => '...',
'prop3' => '...',
],
]
];
This way you're not relying on dirty tricks and do most of the logic where it belongs. The corresponding generation template generation is thus much more pleasing.
{% block myTemplate %}
<table>
{% for row in CompareArray %}
<tr>
{{ _self.buildElementData(row.last ?? {}) }}
<td> #### </td>
{{ _self.buildElementData(row.primary ?? {}) }}
</tr>
{% endfor %}
</table>
{% endblock %}
{% macro buildElementData(item) %}
<td>{{ item.prop1 ?? '' }}</td>
<td>{{ item.prop2 ?? '' }}</td>
<td>{{ item.prop3 ?? '' }}</td>
{% endmacro %}

Get the id of an element of a GROUP_CONCAT?

I made a query to group all the Audits belonging to a specific Area, like in the next query.
SELECT id_Area as idArea, area.Name as area_name, GROUP_CONCAT(id_Audit) as Audits
FROM helios.fsa_audits
LEFT JOIN helios.fsa_areas area USING (id_Area)
WHERE id_Auditor='4'
GROUP BY id_Area;
As result I get this:
I made this to get a datatable in the view like this:
But What I need is to get the id of the Audit at the moment of click each button in the Auditorias.
I'm working with symfony, and I render the datatable like this:
<table aria-describedby="dataTable_info" cellspacing="0" class="table table-hover dataTable" id="dataTable" role="grid" style="width:100%;" width="100%">
<thead>
<tr>
<th>Areas</th>
<th>Auditorias</th>
</tr>
</thead>
<tbody>
{% for audit in auditsByArea %}
<tr>
<td>{{ audit.area_name }}</td>
<td>
{% for i in 1..4 %}
<input class ="auditBtn mx-2" value="{{'W' ~ i }}" href="" data-id="{{ audit.Audits }}" data-area="{{ audit.area_name }}"id="auditBtn" name="auditBtn" type='text' readonly data-toggle="modal" data-target="#auditModal"></input>
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
But it does not put the one id per button(Audit), it assigns all the id's in all the buttons:
data-id="10,11,12,13"
Any idea or suggestion about how to get it?
I'm not in symfony but you should explode your group_concat result
my_array = explode({{ audit.Audits }});
then in you loop refer to the content on the array
<td>
{% for i in 1..4 %}
<input class ="auditBtn mx-2" value="{{'W' ~ i }}" href="" data-id="{{ my_array[i] }}" data-area="{{ audit.area_name }}"id="auditBtn" name="auditBtn" type='text' readonly data-toggle="modal" data-target="#auditModal"></input>
{% endfor %}
</td>

PHP, symfony list data in table

This is a project for school!!!
Let's say that I want to store and show working hours in an HTML table.
I want something like, 'if' the date from DB which user added is equal to the date from the weekly calendar than show me an EDIT button else show me an ADD button?
I want to show Add for the rest of the users who didn't create any data in DB.
<table class="table" id="week">
<thead>
<tr>
<th scope="col">users</th>
{% for day in days %}
<th class="{{ day|date("D-d") }}">
<span>{{ day|date("D") }}</span>
<span>{{ day|date("d") }}</span>
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for user in users %}
<tr class="days">
<th scope="row">{{ users.username }}</th>
{% for day in days %}
<td id="my_cell {{ day|date("d-m") }}">
{% for time in findTime %}
{% if d|date("d-m-Y") == time.date|date('d-m-Y') and users.id == time.getUser().id %}
{{ time.timeFrom|date("H:i") }} - {{ time.timeTo|date("H:i") }}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
I am currently using DB doctrine ORM on symfony 3.4v
I don't have an idea how to solve this problem, I tried with different array function in PHP but I did not find solutions.
Thank you for understanding.

Doctrine - Create dynamical list in View

I am trying to create a view for display the total sales for all the suppliers and customers of a shop.
The total sales for a customer are easy to obtain, the problem is when I have to create the columns with the brands, I have been looking through this site, doctrine, etc, But I cannot find any solution.
SaleController.php
/**
* #Route("/widget/summarize")
*
* #return Render
*/
public function widgetSummarizeAction()
{
$suppliers = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
->select(['su.name AS name'])
->from('AppBundle:Supplier', 'su')
->getQuery()
->getArrayResult()
;
$data = $this->getDoctrine()->getManager()->createQueryBuilder()->groupBy('c.name')
->select([
's.id AS id',
's.invoiceDate AS invoiceDate',
'c.name AS customer',
'SUM(ROUND(s.amount * s.price, 2)) AS totalSales',
'su.name AS supplier',
])
->from('AppBundle:Sale', 's')
->leftJoin('s.supplierCustomer', 'sc')
->leftJoin('sc.customer', 'c')
->leftJoin('sc.supplier', 'su')
->getQuery()
->getArrayResult()
;
return $this->render('AppBundle::home_widget.html.twig', [
'title' => 'Total Sales',
'icon' => 'money',
'urlList' => $this->generateUrl($this->getUrlList()),
'rowRoute' => $this->getUrlEdit(),
'data' => $data,
'columns' => [
'customer' => 'customer',
'totalSales' => 'Total Sales'
]
]);
home_widget.html.twig
<div class="panel panel-default home-widget">
<div class="panel-heading">
<h2 class="panel-title pull-left">
{% if icon is defined and icon is not empty %}<i class="fa fa-{{ icon }}"></i>{% endif %}
{{ title }}
</h2>
{% if urlList %}
<a class="btn btn-primary pull-right" href="{{ urlList }}">See All</a>
{% endif %}
<div class="clearfix"></div>
</div>
<div class="panel-body">
{% if data is not empty and data is iterable %}
<table class="table table-bordered table-striped">
<thead>
<tr>
{% for column in columns %}
<td>{{ column }}</td>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in data %}
{% set href = url(rowRoute, {'id': row.id}) %}
<tr>
{% for key,column in columns %}
<td>
<a href="{{ href }}">
{{ row[key] | raw }}
</a>
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
Maybe try get colulmns name by:
$em = $this->getDoctrine()->getManager();
$columns = $em->getClassMetadata(your_entity_name::class)->getFieldNames();
and loop through the columns in twig template.
<thead>
<tr>
{% for column in columns %}
<td>{{ column }}</td>
{% endfor %}
</tr>
</thead>

How can I order my table's columns using a keyword?

My column is currently {article, formation , tous article , tous formation, notif, offre}. I want to make it in this order {article, tous articles , espace&nbsp, formation , tous formation , &nbsp, notif , offre}
This is my code, the td is named: ban.page
<table id="liste-offresemploi" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th width="5">#</th>
<th>Titre 1</th>
<th>Type</th>
<th>Page</th>
<th>pagetest</th>
<th width="20">Statut</th>
<th>URL</th>
<th width="5"></th>
<th width="5" class="header"></th>
</tr>
</thead>
<tbody>
{% if nb_bannieres > 0 %}
{% for bann in bannieres %}
<tr>
<td>{{ bann.idbanniereinterne }}</td>
<td>{{ bann.titre }}</td>
<td>{% if bann.type == 2 %} Publicité {% else %} {% if bann.type == 1 %} Structure {% endif %} {% endif %}</td>
<td id="tri">{{bann.page }}</td>
<td>{% if bann.page == "articles" %} 0 {% else %} {% if bann.page == "toutarticles" %} 1 {% endif %} {% endif %} </td>
<td><a href="#?" class="toogleactif" name="banniereinterne-{{ bann.idbanniereinterne }}" rel="{% if bann.actif %}0{% else %}1{% endif %}">
{% if bann.actif %}
<button class="btn btn-success actifjs">Actif</button>
{% else %}
<button class="btn btn-warning actifjs">Non Actif</button>
{% endif %}
</a>
</td>
<td>{{ bann.url }}</td>
<td><span class="glyphicon glyphicon-edit btnadmin btnedit" title="Editer" rel="table_banniereinterne|id_{{ bann.idbanniereinterne }}"></span></td>
<td><span class="glyphicon glyphicon-trash btnadmin deladmin" title="Supprimer" rel="{{ bann.idbanniereinterne }}|banniereinterne|0"></span></td>
</tr>
An easy solution is to add in your entity a field order (type int) and give the order value
Category | Order
------------ | ------
article | 2
formation | 3
tous article | 1
And then order in your query by this Order column ASC and you will get
tous article
article
formation

Categories