Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

comment avoir les question d'un quizz dans getQuestions : $pdo->exec(" CREATE TABLE IF…

  1. 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 { pdo−>exec("CREATETABLEIFNOTEXISTSquestions(uuidTEXTPRIMARYKEY,quizidINTEGER,typeTEXTNOTNULL,labelTEXTNOTNULL,choicesTEXT,correctTEXTNOTNULL,FOREIGNKEY(quizid)REFERENCESquizzes(id))");pdo->exec(" CREATE TABLE IF NOT EXISTS questions ( uuid TEXT PRIMARY KEY, quiz_id INTEGER, type TEXT NOT NULL, label TEXT NOT NULL, choices TEXT, correct TEXT NOT NULL, FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ) ");quizzes = json_decode(file_get_contents('data.json'), true);

    foreach (quizzesasquizzes as quiz) { // je regarde si le quizz existe déjà (car si je execute le php il voudra insert et j'aurais un constraint fail) stmt=stmt = pdo->prepare("SELECT * FROM quizzes WHERE id = ?"); stmt−>execute([stmt->execute([quiz['quiz_id']]); exists=exists = 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;
    

    }

  2. Assistant

    The getQuestions function 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 as RadioQuestion, CheckboxQuestion, or TextQuestion. However, to make this work, the INSERT statement when you insert new questions into the questions table is missing the field quiz_id, which is a foreign key that links each question to its respective quiz.

    The following chunk of the code does not include quiz_id in 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_id as a placeholder to the SQL query and pass it as a parameter in the execute method:

    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 getQuestions function to work correctly and return the questions for the specified quiz.

    Regarding your getQuestions function, if you have not already defined the classes RadioQuestion, CheckboxQuestion, and TextQuestion, 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 in getQuestions.