Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions quiz_system_git/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,29 @@

$m_display_ID = 1;

while($m_row = $stmt->fetch()){
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Collect IDs
$questionIDs = [];
foreach ($questions as $q) {
$questionIDs[] = $q['question_id'];
}

// Batch fetch answers
$answersByQuestion = [];
if (!empty($questionIDs)) {
$chunks = array_chunk($questionIDs, 500);
foreach ($chunks as $chunk) {
$placeholders = implode(',', array_fill(0, count($chunk), '?'));
$stmtAns = $pdo->prepare("SELECT * FROM answers WHERE question_id IN ($placeholders)");
$stmtAns->execute($chunk);
while ($row = $stmtAns->fetch(PDO::FETCH_ASSOC)) {
$answersByQuestion[$row['question_id']][] = $row;
}
}
}

foreach ($questions as $m_row){
$m_answers='';
//id var = id column and so on
$m_id = $m_row['id'];
Expand Down Expand Up @@ -296,16 +318,15 @@
}

//gathering answers of question here
$stmtAns = $pdo->prepare("SELECT * FROM answers WHERE question_id=:questionID");
$stmtAns->execute(['questionID' => $m_question_id]);
$currentAnswers = isset($answersByQuestion[$m_question_id]) ? $answersByQuestion[$m_question_id] : [];

$m_answers .= '<tr>
<td></td>
<td>
<ol type="a">
';

while($m_row2 = $stmtAns->fetch()){
foreach ($currentAnswers as $m_row2){
//putting column values in variables
$m_answer = $m_row2['answer'];
$m_correct = $m_row2['correct'];
Expand Down