I am looking for a way to display data from a MySQL database across multiple columns in a similar fashion to a spreadsheet.
I have not started the table design as yet as I am unsure of the best approach to achieve what I would like.
I have considered the following:
TABLE 1 - Homework Task
id (INT)
task (VARCHAR)
This table is just a list of homework tasks
TABLE 2 - Students
id (INT)
studentName (VARCHAR)
This is just a list of students
TABLE 3 - Homework Grades
id (INT)
homeworkTaskId (INT)
studentId (INT)
grade (VARCHAR)
percentage (DECIMAL)
This will hold the marks for each homework task for each student
Now, what I would like to achieve is to be able to display the data like this:
What I am struggling with is my knowledge of SQL commands and knowing if it is possible to retrieve the data in such a way that I can loop through it to display in this format?
I am using PHP with a PDO connection to the MySQL database.
Many thanks in advance for any possible assistance in this matter.
I would use 2 queries, it's much simpler and if your looking it up by the foreign key ( student.id ) in the homework table, it should be fast enough where performance wont be an issue.
$DB = new PDO('mysql:host='.$conf['dbhost'].';dbname='.$conf['dbname'], $conf['dbuser'], $conf['dbpass']);
$students_stmt = $DB->query('SELECT * FROM students');
$homework_stmt = $DB->prepare('
SELECT
h.id, h.grade, h.percentage, ht.task
FROM
homework AS h
JOIN
homework_task AS ht
ON
ht.id = t.homeworkTaskId
WHERE
h.studentId = :student_id
');
$data = [];
$max_homework = 0; //maximum number of homework records
while( false !== ( $student = $students_stmt->fetch(PDO::FETCH_ASSOC) )){
$homework_stmt->execute([':studentId'=>$student['id']]);
$i = 1;
while( false !== ( $homework = $homework_stmt->fetch(PDO::FETCH_ASSOC) )){
$student["homework_$i"] = $homework;
++$i;
}
if( $i > $max_homework ) $max_homework = $i;
$data[] = $student;
}
You'll wind up with an array like this
$data = [ 0 => //first student
[
'studentName' => 'Some Guy',
'homework_1' => [
'grade' => 'A',
'percentage' => '92'
],
'homework_2' => [
'grade' => 'B',
'percentage' => '85'
]
'homework_3' => [ .... ]
], 1 => [ ... //second student ]
];
Then you can loop over $data
and create the table
The $max_homework
is because when you go to display it you will need to know how many homework columns you need ( [group, percentage] ) that way you can create the proper number of headers in your table and fill the students that have less then that. So each row in the table has the same number of cells, if that makes sense. That is if the number of homework records vary from student to student.
By the way this code is untested as I have no way to know the full schema of your tables. It's just an example of the simplest approach ( using PDO ).
This is an example from the php documentation that should get you started. $row will take on the names of the fields in the database because we chose result->fetchassoc
<?
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM table ORDER by ID";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo "<div>".$row['studentname']."</div>";
echo "<div>".$row['studentgrade']."</div>";
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>