简体   繁体   English

如何在Twig HTML表中显示来自递归SQL查询的分组数据?

[英]How to display grouped data from recursive SQL query in a Twig HTML table?

I want to be able to display data from a SQL database (using PDO) and present it in an HTML table, however, I am having trouble understanding how to include a subquery in the results of my original query. 我希望能够显示来自SQL数据库的数据(使用PDO)并将其显示在HTML表中,但是,我很难理解如何在原始查询的结果中包括子查询。 I have gotten close to my desired result by using the GROUP_CONCAT function to group the data as comma separated values. 通过使用GROUP_CONCAT函数将数据分组为逗号分隔的值,我已经接近期望的结果。

Using Twig to populate the HTML table with the desired values, but I can't figure out how to group certain values (course names) underneath the student. 使用Twig用所需的值填充HTML表,但我不知道如何在学生下方对某些值(课程名称)进行分组。

`PHP
SELECT classes.ID, classes.PIDM, classes.fName, classes.lName, classes.advisorOneFirst, classes.advisorOneLast, classes.advisorOneEmail, 
GROUP_CONCAT(classes.courseTitle) AS courses
FROM classes
WHERE term = :cterm AND (classes.MGrade = "F" OR classes.MGrade = "F~" OR classes.MGrade = "D" OR classes.MGrade = "D~")
GROUP BY classes.ID
ORDER BY lName ASC

`HTML
{% for student in classes %}
<tr>
<td>{{student.fName}}</td>
<td>{{student.courses}}</td>
</tr>
{% endfor %}

To illustrate my webpage looks like this: 为了说明我的网页,如下所示:

student1 course1,course2
student2 course3,course4

And I want it to look like this: 我希望它看起来像这样:

student1 course1
         course2
student2 course3
         course4

Also, using 另外,使用

GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")

or any other substitution results in: 或任何其他替代结果:

student1 course1<br>course2
student2 course3<br>course4

Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br> tags wasn't working. Twig会自动转义变量以防止XSS攻击,这就是使用<br>标记无效的原因。 Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>") , update your twig to use the raw filter: 使用GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>") ,更新您的树枝以使用原始过滤器:

{% for student in classes %}
<tr>
<td>{{student.fName}}</td>
<td>{{student.courses|raw}}</td>
</tr>
{% endfor %}

Update 更新

Using the raw filter could produce other issues depending on the characters in courses. 使用原始过滤器可能会产生其他问题,具体取决于课程中的角色。 A better approach would be to use a character you can split on as the separator in group_concat , then split on that character and loop each course in twig. 更好的方法是使用一个可以拆分的字符作为group_concat的分隔符,然后对该字符进行拆分,并在树枝中循环遍历每个路线。 For example, if you used a semicolon as the character separator, 例如,如果您使用分号作为字符分隔符,

Your group concat would look like this: 您的小组会议看起来像这样:

GROUP_CONCAT(classes.courseTitle SEPARATOR ';')

And your twig would like this: 而你的树枝会这样:

{% for student in classes %}
<tr>
    <td>{{ student.fName }}</td>
    <td>
    {% for course in student.courses|split(';') %}
        {{ course }}<br>
    {% endfor %}
    </td>
</tr>
{% endfor %}

given a generalized table: 给定一个广义表:

 myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4

SQL SQL

SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo

HTML HTML

{% for foobar in myTable %}
<tr>
    <td>{{foobar.foo}}</td>
    <td>
    {% for bar in foobar.bars|split("@@@") %}
    {{bar}}<br>
    {% endfor %}
    </td>
</tr>
{% endfor %}

webpage presentation 网页展示

foo1 bar1
     bar3
foo2 bar2
     bar4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM