PHP, symfony list data in table - php

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.

Related

Symfony Twig datatable

I'm building a Product Information Manager (PIM). Trying to build this with the symfony framework. The problem I'm facing at the moment. I'm showing my product information with datatables with the twig file example below.
{% set title = 'Product List' %}
{% extends 'table.twig' %}
{% block h1_page_header %} Product List {% endblock %}
{% block panel_heading %} Product List {% endblock %}
{% block thead %}
<tr>
<th>ID</th>
<th>Name</th>
<th>SKU</th>
<td>Actions</td>
</tr>
{% endblock %}
{% block tbody %}
{% for product in products %}
<tr>
<td>
{{ product.id }}
</td>
<td>
{{ product.name }}
</td>
<td>
SKU
</td>
<td>
<a href="{{ path('app_product_getproduct', {'id': product.id}) }}" class="btn btn-success btn-sm" >
<span class="glyphicon glyphicon-pencil"></span>
</a>
<a href="{{ path('app_product_delete', {'id': product.id}) }}" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
{% endfor %}
The problem I have now is that my database has now almost 60-70K. But my product/list/ page is not rendering due to the 70K product which I want to show in datatables. It works fine for few 2-4k Products but above this it does not render.
I did a research and found out that I can resolve my issue with server side processing. But the question I have is there a better solution / alternative to achieve my goal?
Even if server-side processing will help, some navigators may struggle to render all those elements at once. From a user point of view, that not interesting nether. Nobody will ever scroll through all the 70k products. Users are lazy :)
You should implement pagination, search-bar and filters to render a easy readable short list.
If you like JS you can implement a more elegant solution, like an infinite scroll.

Can't include a template multiple time in Twig

I need to include a template multiple time in Twig.
Here is an explanation of my case:
I have the original template, lets say index.html.twig. This template include diffirent templates : part1.html.twig part2.html.twig etc..
My goal is to include a final template extra.html.twig to each sub templates (part1, part2). I've made the test, who concluded that I can include the final template just once. Twig detects that this template is already concluded and don't display it twice or three.
How can I achieve this result ?
|index.html.twig
|include part1.html.twig
|include extra.html.twig
|include part2.html.twig
|include extra.html.twig
UPDATE :
{% block file_upload %}
<div class="col-sm-12">
<div class="upload_files" style="float: right">
{{ form_widget(upload_form.button) }}
</div>
{{ form_widget(upload_form.attachment) }}
<table class="table table-files-preview" hidden>
<thead>
<td>{% trans %}NAME{% endtrans %}</td>
<td>{% trans %}TYPE{% endtrans %}</td>
<td>{% trans %}SIZE{% endtrans %}</td>
</thead>
<tbody class="tbody-file-preview">
</tbody>
</table>
<input type="hidden" id="files_source" value="{{ source }}">
</div>
{% endblock %}

Twig if/else not working

I'm not sure why my code isn't executing. I know that it should be working this way but all that happens now is that it doesn't do the else portion.
In debugging I know descriptions is not null and descriptions show for those that have it.
{% if descriptions is not null %}
{{ dump(descriptions) }}
{% for description in descriptions %}
<td>{{ description.productDesciption }}</td>
{% endfor %}
{% else %}
<td>
Create a Description for this Product
</td>
{% endif %}
You can simplify using the The else Clause of the for statement:
{% for description in descriptions %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}
Hope this help
You can use if into for loop.
{% for description in descriptions if descriptions is not null %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}

Twig doesn't approximate number

I have a strange situation. My code is :
{% set total_amount=0 %}
{% for result in a_result %}
<tr>
<td>{% set total_amount=total_amount+("%.2f"|format(result.tva*result.prix_ht)) %}
{{ "%.2f"|format(result.tva*result.prix_ht) }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount }}</td>
</tr>
As result I have :
15.98, 25.49, 25.49
And Total = 65 but total should be equal with 65.96. I don't understand where is the problem. Can you help me please ?
I suggest you to use the round and number_format filter as follow:
{% set total_amount=0 %}
{% for result in a_result %}
{% set value = (result.tva* result.prix_ht)|round(2) %}
{% set total_amount=total_amount+value %}
<tr>
<td>
{{ value|number_format(2, '.', ',') }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount|number_format(2, '.', ',') }}</td>
</tr>
A running example with sample data in this twigfiddle files
Hope this help

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