html - I need to dynamically assign radio button in a php file quiz

I'm designing a dynamically generated quiz page (using mysqli / php / html) and I need to insert some radio buttons in order to validate the answers. The problem that I have is that when printing the radio buttons inside the loop, they do not uncheck properly - instead of having 4 sets of radio buttons / question, my radio buttons are assigned like this: question 1 a) with question 2 a) and so on. Does anybody have any idea on how to remedy this?

$sql = "SELECT id, descr, a,b,c,d FROM question ORDER BY rand() LIMIT  15";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        print "<strong>".$row["descr"] ."</strong><br>";
        echo "<br>";
        echo 'a:'.$row["a"].'<td><cb><input type="radio" name="a" value="$row["a"]"></cb></td>'."<br>";
        echo "<br>";
        echo 'b:'. $row["b"].'<td><cb><input type="radio" name="b" value="$row["b"]"></cb></td>'."<br>";
        echo "<br>";
        echo 'c:'.$row["c"].'<td><cb><input type="radio" name="c" value="$row["c"]"></cb></td>'."<br>";
        echo "<br>";
        echo 'd:'. $row["d"].'<td><cb><input type="radio" name="d" value="$row["d"]"></cb></td>'."<br>";
        echo "<br>";

    }

} else {
    echo "0 results";
}
$conn->close();

2 Answers

  1. Edward- Reply

    2019-11-13

    You will need to include something unique to each question in the radio button name attribute, such as:

    <input type="radio" name="a<?= $row['question_number'] ?" />
    

    question_number is just some made up field but this should be something that is unique to each question. This would result in something like:

    Question 1:    
    <input type="radio" name="a1" />
    <input type="radio" name="b1" />
    <input type="radio" name="c1" />
    <input type="radio" name="d1" />
    
    Question 2:
    <input type="radio" name="a2" />
    <input type="radio" name="b3" />
    <input type="radio" name="c4" />
    <input type="radio" name="d5" />
    
  2. Edwin- Reply

    2019-11-13

    Do the following :

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            print "<strong>".$row["descr"] ."</strong><br>";
            echo "<br>";
            echo 'a:'.$row["a"].'<td><cb><input type="radio" name="q'.$row["id"].'" value="a"></cb></td>'."<br>";
            echo "<br>";
            echo 'b:'. $row["b"].'<td><cb><input type="radio" name="q'.$row["id"].'" value="b"></cb></td>'."<br>";
            echo "<br>";
            echo 'c:'.$row["c"].'<td><cb><input type="radio" name="q'.$row["id"].'" value="c"></cb></td>'."<br>";
            echo "<br>";
            echo 'd:'. $row["d"].'<td><cb><input type="radio" name="q'.$row["id"].'" value="d"></cb></td>'."<br>";
            echo "<br>";
    
        }
    
    } else {
        echo "0 results";
    }
    

    This way each question creates a group of radio buttons having the same name and differing in their value, therefore when you submit you can check e.g.: $_GET["qXXX"] to determine what the answer was for question XXX (e.g. a, b, c, or d )

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>