从数据库检查php echo

时间:2012-10-01 13:21:25

标签: php echo checked

我正在尝试根据已输入数据库的用户值添加“已检查的语句”。

我将数据库端设计为q1,q2等,并且只有一个带有“a”,“b”或“c”的varchar。

我基本上希望代码检索答案并将其放入复选框中作为“已检查”,由于某种原因我无法让它工作。

到目前为止,我的代码是:

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

?>

我可以在while函数中进行查询,只需让每个输入类型说if($ row ['1'] ='a')echo“checked ='checked'”;或者我错了吗?

任何帮助都会很棒。 感谢

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分:0)

以这种方式试试

 <p><input type="radio" name="q<?php echo $id; ?>" value="a" <?php if($opt1=='a') echo "checked='checked'"; ?>><?php echo $opt1; ?> </p>

答案 3 :(得分: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)相同

答案 4 :(得分:0)

假设$answer字段是'a','b','c'或'd',这应该有效:

<p><input type=radio name='q<?php echo $id; ?>' value="a" <?php if($answer == 'a'){ echo "checked='checked'";} ?>> <?php echo $opt1; ?> </p>