I'm trying to add a "checked statement" based upon the users value already entered into the database.
I've designed the database side as q1,q2 etc and have a single varchar with an "a","b", or "c".
I basically want the code to retrieve the answer and put it into the checkbox as "checked", for some reason I just can't get it to work.
my code so far is:
<?php
//retreive questions from database and put into question box
$query2 = "SELECT `QuestionId`, `Question`, `Opt1`, `Opt2`, `Opt3`, `Opt4`,`Answer` FROM `pf_questions`";
$question2 = mysql_query($query2);
while($row = mysql_fetch_array($question2)){
$id = $row['QuestionId'];
$question = $row['Question'];
$opt1 = $row['Opt1'];
$opt2 = $row['Opt2'];
$opt3 = $row['Opt3'];
$opt4 = $row['Opt4'];
$answer = $row["Answer"];
?>
<div id="ContainerQuestion">
<span class="Question">Question <?php echo $id; ?>. <?php echo $question; ?></span>
<p><input type=radio name='q<?php echo $id; ?>' value="a"> <?php echo $opt1; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="b"> <?php echo $opt2; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="c"> <?php echo $opt3; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="d"> <?php echo $opt4; ?> </p>
</div>
<?php
}
?>
Can I do the query inside the while function and just have each input type say if($row['1'] = 'a') echo "checked='checked'"; or am I wrong?
Any help will be awesome. thanks