How to display grouped data from recursive SQL query in a Twig HTML table?How to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?SQL update from one Table to another based on a ID matchHow can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?How to drop a table if it exists in SQL Server?SQL query return data from multiple tablespeewee: filter select query results from many to many relationshipHow do I pass variables and data from PHP to JavaScript?Inserting Data to a Table Retrieved from Two Merged Tables
What do these commands specifically do?
How to gently end involvement with an online community?
When, exactly, does the Rogue Scout get to use their Skirmisher ability?
Where can/should I, as a high schooler, publish a paper regarding the derivation of a formula?
How long do you think advanced cybernetic implants would plausibly last?
Can RMSE and MAE have the same value?
Does ostensible/specious make sense in this sentence?
Does this VCO produce a sine wave or square wave
How is linear momentum conserved in case of a freely falling body?
Hangman game in Python - need feedback on the quality of code
If the Shillelagh cantrip is applied to a club with non-standard damage dice, what is the resulting damage dice?
Boot Windows from SAN
Papers on arXiv solving the same problem at the same time
How can I download a file through 2 SSH connections?
Round towards zero
How to obtain a polynomial with these conditions?
How do we tell which part of kinetic energy gives rise to temperature?
Changing JPEG to RAW to use on Lightroom?
How many birds in the bush?
What does "rel" in `mathrel` and `stackrel` stands for?
How does encoder decoder network works?
Could this kind of inaccurate sacrifice be countered?
Handling Disruptive Student on the Autism Spectrum
What is a natural problem in theory of computation?
How to display grouped data from recursive SQL query in a Twig HTML table?
How to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?SQL update from one Table to another based on a ID matchHow can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?How to drop a table if it exists in SQL Server?SQL query return data from multiple tablespeewee: filter select query results from many to many relationshipHow do I pass variables and data from PHP to JavaScript?Inserting Data to a Table Retrieved from Two Merged Tables
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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. I have gotten close to my desired result by using the GROUP_CONCAT function to group the data as comma separated values.
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.
`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
php sql twig
add a comment |
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. I have gotten close to my desired result by using the GROUP_CONCAT function to group the data as comma separated values.
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.
`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
php sql twig
add a comment |
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. I have gotten close to my desired result by using the GROUP_CONCAT function to group the data as comma separated values.
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.
`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
php sql twig
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. I have gotten close to my desired result by using the GROUP_CONCAT function to group the data as comma separated values.
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.
`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
php sql twig
php sql twig
edited Mar 27 at 22:04
user19550
asked Mar 27 at 19:25
user19550user19550
33 bronze badges
33 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br>
tags wasn't working. Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")
, update your twig to use the raw filter:
% for student in classes %
<tr>
<td>student.fName</td>
<td>student.courses</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. 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>
split(';') %
course <br>
% endfor %
</td>
</tr>
% endfor %
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
add a comment |
given a generalized table:
myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4
SQL
SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo
HTML
% for foobar in myTable %
<tr>
<td>foobar.foo</td>
<td>
% for bar in foobar.bars
bar<br>
% endfor %
</td>
</tr>
% endfor %
webpage presentation
foo1 bar1
bar3
foo2 bar2
bar4
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385046%2fhow-to-display-grouped-data-from-recursive-sql-query-in-a-twig-html-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br>
tags wasn't working. Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")
, update your twig to use the raw filter:
% for student in classes %
<tr>
<td>student.fName</td>
<td>student.courses</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. 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>
split(';') %
course <br>
% endfor %
</td>
</tr>
% endfor %
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
add a comment |
Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br>
tags wasn't working. Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")
, update your twig to use the raw filter:
% for student in classes %
<tr>
<td>student.fName</td>
<td>student.courses</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. 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>
split(';') %
course <br>
% endfor %
</td>
</tr>
% endfor %
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
add a comment |
Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br>
tags wasn't working. Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")
, update your twig to use the raw filter:
% for student in classes %
<tr>
<td>student.fName</td>
<td>student.courses</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. 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>
split(';') %
course <br>
% endfor %
</td>
</tr>
% endfor %
Twig automatically escapes your variables to protect against XSS attacks, which is why using the <br>
tags wasn't working. Using the GROUP_CONCAT(classes.courseTitle SEPARATOR "<br>")
, update your twig to use the raw filter:
% for student in classes %
<tr>
<td>student.fName</td>
<td>student.courses</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. 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>
split(';') %
course <br>
% endfor %
</td>
</tr>
% endfor %
edited Apr 17 at 2:08
answered Mar 27 at 21:32
patrick3853patrick3853
5102 silver badges7 bronze badges
5102 silver badges7 bronze badges
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
add a comment |
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
I won't be able to test this until tomorrow, but thanks for the help! Will definitely use the safer, second approach. Is there any reason to not use |split(',') without SEPARATOR ';' or are you just trying to avoid edge cases like "Hist. of Mid,Late- Inst'l Art"?
– user19550
Mar 27 at 22:17
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like
~|~
– patrick3853
Apr 4 at 20:07
@user19550 yes, I used a semicolon because it seems like a safer character to split on. What character you use depends on your data. You should use something that will never be in the course title. You can also use multiple characters if needed, something like
~|~
– patrick3853
Apr 4 at 20:07
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
tested and works with a minor change: % for course in courses should be split(';') % marked as answered!
– user19550
Apr 16 at 3:51
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
@user19550I Thanks for the update, I've updated the answer with the minor correction you pointed out. FYI, you should accept the answer so future readers know it is the correct answer.
– patrick3853
Apr 17 at 2:15
add a comment |
given a generalized table:
myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4
SQL
SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo
HTML
% for foobar in myTable %
<tr>
<td>foobar.foo</td>
<td>
% for bar in foobar.bars
bar<br>
% endfor %
</td>
</tr>
% endfor %
webpage presentation
foo1 bar1
bar3
foo2 bar2
bar4
add a comment |
given a generalized table:
myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4
SQL
SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo
HTML
% for foobar in myTable %
<tr>
<td>foobar.foo</td>
<td>
% for bar in foobar.bars
bar<br>
% endfor %
</td>
</tr>
% endfor %
webpage presentation
foo1 bar1
bar3
foo2 bar2
bar4
add a comment |
given a generalized table:
myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4
SQL
SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo
HTML
% for foobar in myTable %
<tr>
<td>foobar.foo</td>
<td>
% for bar in foobar.bars
bar<br>
% endfor %
</td>
</tr>
% endfor %
webpage presentation
foo1 bar1
bar3
foo2 bar2
bar4
given a generalized table:
myTable
=========
foo | bar
----+----
foo1|bar1
foo2|bar2
foo1|bar3
foo2|bar4
SQL
SELECT foo,
GROUP_CONCAT(bar SEPARATOR "@@@") AS bars
FROM myTable
GROUP BY foo
HTML
% for foobar in myTable %
<tr>
<td>foobar.foo</td>
<td>
% for bar in foobar.bars
bar<br>
% endfor %
</td>
</tr>
% endfor %
webpage presentation
foo1 bar1
bar3
foo2 bar2
bar4
edited Mar 28 at 14:56
answered Mar 28 at 1:12
user19550user19550
33 bronze badges
33 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385046%2fhow-to-display-grouped-data-from-recursive-sql-query-in-a-twig-html-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown