I have a MYSQL database storing levels for students for each terms work.
I'm fine creating the php page, with query etc and very comfortable with the hmtl/css required to create tables. I have a basic understanding of arrays.
Where I am struggling is how to take each students data and display it in columns in the html table.
Previously my assessment table in the database had multiple colums - aut7, spr7, sum7 etc so it was easy to display in an html table. However I have been trying to make the database more effective so am now storing each assessment as a separate record - database is much better set-up but has thrown up the following problem (well lack of knowledge on my part!!)
A simple example of what I mean is as follows:
This is the result of a query:
student_id term level
1 aut7 3b
1 spr7 3a
1 sum7 4c
2 aut7 3a
2 spr7 4c
2 sum7 4b
I would like it in displayed in my html/css table as follows:
Student Aut7 Spr7 Sum7
1 3b 3a 4c
2 3a 4c 4a
If anyone could help a relative newcomer to PHP/MySQL I would be very grateful.
You need to pivot your results. Unfortunately MySQL does have PIVOT
but you can build a query like this to achieve the same results
SELECT student_id,
MAX(CASE WHEN term = 'aut7' THEN level END) Aut7,
MAX(CASE WHEN term = 'spr7' THEN level END) Spr7,
MAX(CASE WHEN term = 'sum7' THEN level END) Sum7
FROM student_terms
GROUP BY student_id
See demo in SQL Fiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.