comment avoir les question d'un quizz dans getQuestions : $pdo->exec(" CREATE TABLE IF…
- User
comment avoir les question d'un quizz dans getQuestions : $pdo->exec(" CREATE TABLE IF NOT EXISTS quizzes ( id INTEGER PRIMARY KEY, name TEXT NOT NULL ) ");
try { quizzes = json_decode(file_get_contents('data.json'), true);
foreach (quiz) { // je regarde si le quizz existe déjà (car si je execute le php il voudra insert et j'aurais un constraint fail) pdo->prepare("SELECT * FROM quizzes WHERE id = ?"); quiz['quiz_id']]); stmt->fetch(PDO::FETCH_ASSOC);
// si le quizz n'existe pas, je l'insert donc if (!$exists) { $pdo->prepare(" INSERT INTO quizzes (id, name) VALUES (?, ?) ")->execute([ $quiz['quiz_id'], $quiz['quiz_name'], ]); } foreach ($quiz['questions'] as $question) { $question['quiz_id'] = $quiz['quiz_id']; // je ne comprend pas pourquoi je n'ai pas la clé étrangère // quiz_id dans mon tableau question alors que dans la table de la bd // j'ai bien la clé étrangère quiz_id $stmt = $pdo->prepare("SELECT * FROM questions WHERE uuid = ?"); $stmt->execute([$question['uuid']]); $exists = $stmt->fetch(PDO::FETCH_ASSOC); // meme chose pour les questions if (!$exists) { $pdo->prepare(" INSERT INTO questions (uuid, type, label, choices, correct) VALUES (?, ?, ?, ?, ?) ")->execute([ $question['uuid'], $question['type'], $question['label'], isset($question['choices']) ? json_encode($question['choices']) : null, is_array($question['correct']) ? json_encode($question['correct']) : $question['correct'], ]);function getQuestions($quiz_id) { $pdo = new PDO('sqlite:db.sqlite'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare("SELECT * FROM questions WHERE quiz_id = ?"); $stmt->execute([$quiz_id]); $datas = $stmt->fetchAll(PDO::FETCH_ASSOC); $questions = []; foreach ($datas as $data) { $question = null; $uuid = $data['uuid']; $type = $data['type']; $label = $data['label']; $choices = json_decode($data['choices'], true); $correct = is_array(json_decode($data['correct'], true)) ? json_decode($data['correct'], true) : $data['correct']; switch ($type) { case 'radio': $question = new RadioQuestion($uuid, $type, $label, $choices, $correct); break; case 'checkbox': $question = new CheckboxQuestion($uuid, $type, $label, $choices, $correct); break; case 'text': $question = new TextQuestion($uuid, $type, $label, $correct); break; } if ($question !== null) { $questions[] = $question; } } return $questions;}
- Assistant
The
getQuestionsfunction you posted is designed to retrieve all the questions for a specific quiz from the database and instantiate corresponding question objects depending on the type of question such asRadioQuestion,CheckboxQuestion, orTextQuestion. However, to make this work, the INSERT statement when you insert new questions into thequestionstable is missing the fieldquiz_id, which is a foreign key that links each question to its respective quiz.The following chunk of the code does not include
quiz_idin the INSERT statement:$pdo->prepare(" INSERT INTO questions (uuid, type, label, choices, correct) VALUES (?, ?, ?, ?, ?) ")->execute([ $question['uuid'], $question['type'], $question['label'], isset($question['choices']) ? json_encode($question['choices']) : null, is_array($question['correct']) ? json_encode($question['correct']) : $question['correct'], ]);To correct this, you should add
quiz_idas a placeholder to the SQL query and pass it as a parameter in theexecutemethod:Replace the above code with the following:
$pdo->prepare(" INSERT INTO questions (uuid, quiz_id, type, label, choices, correct) VALUES (?, ?, ?, ?, ?, ?) ")->execute([ $question['uuid'], $question['quiz_id'], // this is the missing piece $question['type'], $question['label'], isset($question['choices']) ? json_encode($question['choices']) : null, is_array($question['correct']) ? json_encode($question['correct']) : $question['correct'], ]);Now, each question will have a link to its respective quiz_id which allows the
getQuestionsfunction to work correctly and return the questions for the specified quiz.Regarding your
getQuestionsfunction, if you have not already defined the classesRadioQuestion,CheckboxQuestion, andTextQuestion, you need to define them to match the structure needed for your application. Otherwise, you will receive a class not found error. Make sure that these classes or similar ones are defined and that they have constructors that can accept the parameters you're passing ingetQuestions.