So, I have this scenario of a quiz where questions are attached to groups and so when someone do a quiz, first an attempt is stored in a table and then as person does quiz e.g of 3 questions, its points are recorded with points, question, group and quiz attempt id in a table. Here are my table structure:
Table attempts
id | status ( 0 for not complete, 1 for complete )
1 | 1
2 | 1
3 | 0
Table groups
id | title
1 | Group 1
2 | Group 2
3 | Group 3
Table points
id | attempt_id | group_id | points
1 | 1 | 2 | 5
2 | 1 | 2 | 5
3 | 1 | 1 | 5
4 | 2 | 3 | 5
5 | 2 | 2 | 5
6 | 2 | 3 | 5
7 | 3 | 1 | 5
8 | 3 | 3 | 5
I need to show the group list with sum of each group from points table infront of them but only from the completed attempts from attempts table:
Here's how I am trying:
$query = mysqli_query($conn, "SELECT * FROM `groups` order BY `id` ASC");
while ( $row = mysqli_fetch_array($query) ) {
$group_id = $row["id"];
$gpc = mysqli_query($conn, "SELECT SUM(`points`) as TotalPoints FROM `points` WHERE `attempt_id` IN (SELECT `id` FROM `attempts` WHERE `status`=1) AND `group_id`=".$group_id);
$gpcrow = mysqli_fetch_assoc($gpc);
echo $row["title"]." : ".$gpcrow["TotalPoints"];
}
Result should be:
Group 1 : 5
Group 2 : 15
Group 3 : 10
But its not giving me exact results as its giving me results from all complete and incomplete attempts. But I need results only from completed attempts in group listing shape.