问题
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
回答1:
<p><input type=radio name='q<?php echo $id; ?>' <?php echo $answer == 'a'?'checked="checked"':'' ?> value="a"> <?php echo $opt1; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?php echo $answer == 'b'?'checked="checked"':'' ? value="b"> <?php echo $opt2; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?php echo $answer == 'c'?'checked="checked"':'' ? value="c"> <?php echo $opt3; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?php echo $answer == 'd'?'checked="checked"':'' ? value="d"> <?php echo $opt4; ?> </p>
回答2:
Change the radio buttons to:
<p><input type=radio name='q<?php echo $id; ?>' value="a" <?php echo ($row[1] == 'a') ? 'selected="selected"':''; ?>> <?php echo $opt1; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="b" <?php echo ($row[1] == 'b') ? 'selected="selected"':''; ?>> <?php echo $opt2; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="c" <?php echo ($row[1] == 'c') ? 'selected="selected"':''; ?>> <?php echo $opt3; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' value="d" <?php echo ($row[1] == 'd') ? 'selected="selected"':''; ?>> <?php echo $opt4; ?> </p>
回答3:
Try it this way
<p><input type="radio" name="q<?php echo $id; ?>" value="a" <?php if($opt1=='a') echo "checked='checked'"; ?>><?php echo $opt1; ?> </p>
回答4:
Assuming that the $answer
fields are either 'a', 'b', 'c', or 'd', this should work:
<p><input type=radio name='q<?php echo $id; ?>' value="a" <?php if($answer == 'a'){ echo "checked='checked'";} ?>> <?php echo $opt1; ?> </p>
回答5:
you can simply check if the answer match to the option or not by using
<?=( $answer == 'a')?("checked='checked'"):(""); ?>
if the answer is same to the option echo checked='checked'
else empty string
Full code
<?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; ?>' <?=( $answer == 'a')?("checked='checked'"):(""); ?> value="a"> <?php echo $opt1; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?=( $answer == 'b')?("checked='checked'"):(""); ?> value="b"> <?php echo $opt2; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?=( $answer == 'c')?("checked='checked'"):(""); ?> value="c"> <?php echo $opt3; ?> </p>
<p><input type=radio name='q<?php echo $id; ?>' <?=( $answer == 'd')?("checked='checked'"):(""); ?> value="d"> <?php echo $opt4; ?> </p>
</div>
<?php
}
?>
NOTE Assuming that your answer is stored in the $row["Answer"];
is same as option (a,b,c,d)
来源:https://stackoverflow.com/questions/12674147/php-echo-checked-from-database