Skip to content
Advertisement

creating a quiz with php and pdo

I’ve created a quiz for my website, the admin can sign in and change the questions or add questions etc.

For the quiz page I’m taking the information down like so:

try {
    $stmt = $db->query('SELECT questionID, question, optionA, optionB, '  
        . ' optionC, optionD FROM quiz WHERE editedBy = "admin" ORDER BY questionID ');
    while($row = $stmt->fetch()){

        echo '<h3>'.$row['question'].'</h3>';
        echo '<ol>';
        echo '<li><input type="radio" name="q1" value = '.$row['optionA'].'/> '.$row['optionA'].'</li>';
        echo '<li><input type="radio" name="q1"  value = '.$row['optionB'].'/> '.$row['optionB'].'</li>';
        echo '<li><input type="radio" name="q1"   value = '.$row['optionC'].'/> '.$row['optionC'].'</li>';
        echo '<li><input type="radio" name="q1" value = '.$row['optionD'].'/> '.$row['optionD'].'</li>';
        echo '</ol>';
    }

} catch(PDOException $e) {
    echo $e->getMessage();
}

Obviously the way it’s done at the moment every question will have the value “q1”, so I was wondering is there a way I can change this so that it could be the first question taken down will have the value q1 for the radio buttons, 2nd question will have the value q2 and so on. Haven’t been able to find anything on how to day so was wondering if it was actually possible or have I just gone about making my quiz the wrong way?

Advertisement

Answer

Another option (then the options mentioned by @waterloomatt) is to use questionID as the name, this makes matching the given answers back to the right question very easy.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement