I
I
Ilya Loopashko2021-01-27 16:58:03
Twig
Ilya Loopashko, 2021-01-27 16:58:03

How to arrange the data in the table?

There is an entry in the database:

name: Ivan
Day: Tuesday
Project: 1

name: Gennady
Day: Saturday
Project: 2

How can I enter this data into the table, what would happen:
601170ccc3015517878631.jpeg

My code:

<table class="table">
<thead>
    <tr>
    <th scope="col">Проект</th>
    <th scope="col">Пн</th>
    <th scope="col">Вт</th>
    <th scope="col">Ср</th>
    <th scope="col">Чт</th>
    <th scope="col">Пт</th>
    <th scope="col">Сб</th>
    <th scope="col">Вс</th>
    </tr>
</thead>

{% for user in users %}
<tbody>
    <tr>
    <th scope="row">{{ user.project }}</th>
    Тут нужно добавить код...
    </tr>


</tbody>

{% endfor %}
</table>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-01-27
@rozhnev

You can solve this problem with a database:

select 
  projects.id,
  GROUP_CONCAT(if(day=1, name, null)) as Monday,
  GROUP_CONCAT(if(day=2, name, null)) as Tuesday,
  GROUP_CONCAT(if(day=3, name, null)) as Wednesday,
  GROUP_CONCAT(if(day=4, name, null)) as Thursday,
  GROUP_CONCAT(if(day=5, name, null)) as Friday,
  GROUP_CONCAT(if(day=6, name, null)) as Saturday,
  GROUP_CONCAT(if(day=7, name, null)) as Sunday
from projects
group by projects.id
;

Result:
+====+========+=========+===========+==========+========+==========+========+
| id | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday |
+====+========+=========+===========+==========+========+==========+========+
| 1  | John   |         |           |          |        |          |        |
+----+--------+---------+-----------+----------+--------+----------+--------+
| 2  |        |         |           | Jack,Mike|        |          |        |
+----+--------+---------+-----------+----------+--------+----------+--------+

Check SQL query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question