Q:

Pre-populate a lot of radio button group from database

I'm working on a project. I want to pre-populated data from database.
HTML
<div class="question">
  <input type="radio" name="q1" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q1" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q1" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
  <input type="radio" name="q2" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q2" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q2" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
  <input type="radio" name="q3" value="A" <?php echo $checkedA ?>>
  <input type="radio" name="q3" value="B" <?php echo $checkedB ?>>
  <input type="radio" name="q3" value="C" <?php echo $checkedC ?>>
</div>
<!-- etc till let's say 30 question -->

PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
for($i=1; $i<=30; $i++){
  switch($data[${"answer".$i}]{
   case "A" : $checkedA="checked"; break;
   case "B" : $checkedB="checked"; break;
   case "C" : $checkedC="checked"; break;
  }
}

Then, how to make the q1 which is corresponded with $data['answer1'] checked if it is filled with data from database, etc?

A:

Using the code you've provided, It's viable to get the data from the database and then show the result in your html and not over complicate things. To pre-populate your radio buttons, you need to have the following below:
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);

HTML
<div class="question">
  <input type="radio" name="q1" value="A" <?php echo ($data['q1'] == 'A')? 'checked' : '';  ?>>
  <input type="radio" name="q1" value="B" <?php echo ($data['q1'] == 'B')? 'checked' : '';  ?>>>
  <input type="radio" name="q1" value="C" <?php echo ($data['q1'] == 'C')? 'checked' : '';  ?>>
</div>
<div class="question">
  <input type="radio" name="q2" value="A" <?php echo ($data['q2'] == 'A')? 'checked' : '';  ?>>
  <input type="radio" name="q2" value="B" <?php echo ($data['q2'] == 'B')? 'checked' : '';  ?>>
  <input type="radio" name="q2" value="C" <?php echo ($data['q2'] == 'C')? 'checked' : '';  ?>>
</div>
<div class="question">
  <input type="radio" name="q3" value="A" <?php echo ($data['q3'] == 'A')? 'checked' : '';  ?>>
  <input type="radio" name="q3" value="B" <?php echo ($data['q3'] == 'B')? 'checked' : '';  ?>>
  <input type="radio" name="q3" value="C" <?php echo ($data['q3'] == 'C')? 'checked' : '';  ?>>
</div>

With that piece of code, it sets checked depending on what the value coming from the database is and displays it on the screen.

