0

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

4

5 回答 5

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>
于 2012-10-01T13:28:30.873 回答
0

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>
于 2012-10-01T13:30:25.493 回答
0

将单选按钮更改为:

<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>
于 2012-10-01T13:25:51.963 回答
0

试试这个方法

 <p><input type="radio" name="q<?php echo $id; ?>" value="a" <?php if($opt1=='a') echo "checked='checked'"; ?>><?php echo $opt1; ?> </p>
于 2012-10-01T13:26:02.680 回答
0

您可以通过使用简单地检查答案是否与选项匹配

<?=( $answer  == 'a')?("checked='checked'"):(""); ?> 

如果答案与选项 echo 相同,则为checked='checked'空字符串

完整代码

   <?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
    }

    ?>

注意假设您的答案存储在$row["Answer"];与选项(a,b,c,d)相同

于 2012-10-01T13:26:16.133 回答