I have a database table for record classes attendance in school. like this:database table
code | name | date | class | attendance |
---|---|---|---|---|
101 | john | 01-03-2023 | math | absent |
101 | john | 01-03-2023 | geometry | Present |
101 | john | 02-03-2023 | math | Closed |
101 | john | 02-03-2023 | geometry | Present |
102 | smith | 01-03-2023 | Geography | Present |
102 | smith | 01-03-2023 | History | Present |
102 | smith | 01-03-2023 | literature | Closed |
102 | smith | 02-03-2023 | Geography | Present |
102 | smith | 02-03-2023 | History | absent |
102 | smith | 02-03-2023 | literature | absent |
I run query (MYSQL) with php for count that show data as group by name and classes in html table. like this:html table
name | class | Present | absent | closed |
---|---|---|---|---|
john | math | 0 | 1 | 1 |
john | geometry | 2 | 0 | 0 |
smith | Geography | 2 | 0 | 0 |
smith | History | 1 | 1 | 0 |
smith | literature | 1 | 0 | 1 |
I want add row below each name that count sum of her attendance. like this:i want this
name | class | Present | absent | closed |
---|---|---|---|---|
john | math | 0 | 1 | 1 |
john | geometry | 2 | 0 | 0 |
john | 2 | 1 | 1 | |
smith | Geography | 2 | 0 | 0 |
smith | History | 1 | 1 | 0 |
smith | literature | 1 | 0 | 1 |
smith | 4 | 1 | 1 |
please guide me!
I tried ROLLUP but that work only for number not string like absent, presnt ...
Last Update:this is my php code:
<?php$sql = "SELECT code, name, class FROM attendance GROUP BY name, class";$result = $mysqli -> query($sql);$row = $result ->fetch_assoc();?><table><thead><tr><th>Name</th><th>class</th><th>Present</th><th>absent</th><th>closed</th></tr></thead><tbody><?php foreach($row as $k) { $code = $row["code"]; $class = $row["class"]; ?><tr><td><?php echo $row["name"];?></td><td><?php echo $row["class"];?></td><td><?php $sql2 = "SELECT count(*) from attendance WHERE code = '$code' AND class = '$class' AND attendance = 'Present'"; $result2 = $mysqli -> query($sql2); $count2= $result2 ->num_rows; echo $count2; ?></td><td><?php $sql3 = "SELECT count(*) from attendance WHERE code = '$code' AND class = '$class' AND attendance = 'absent'"; $result3 = $mysqli -> query($sql3); $count3= $result3 ->num_rows; echo $count3; ?></td><td><?php $sql4 = "SELECT count(*) from attendance WHERE code = '$code' AND class = '$class' AND attendance = 'closed'"; $result4 = $mysqli -> query($sql4); $count4= $result4 ->num_rows; echo $count4; ?></td></tr><?php } ?></tbody></table>