放置所选行而不重复

时间:2015-08-14 07:24:12

标签: php

我从数据库中选择了一些值(基本上是测验问题),我试图将每个问题放在带有输入值的标题中。
但是我的$row[0]语法给了我重复。对于每个问题我都有相同的答案:0-1-2-3-4。

我创建此部分的代码一次:

<li>
    <h3>'$row[0]'</h3>
    <input type='radio' name='question-1-answers' id='question-1-answers-A' value='A' />
    <label for='question-1-answers-A'>A) Computer Styled Sections </label>
    <input type='radio' name='question-1-answers' id='question-1-answers-B' value='B' />
    <label for='question-1-answers-B'>B) Cascading Style Sheets</label>
</li>

如出现的所有问题及其下方的答案:

if ($result = $mysqli->query("SELECT question FROM questions WHERE type='$type'")) {
    while($row = $result->fetch_array()) {
        $quiz="<form action='' method='post' id='quiz'>
            <ol>
                <li>
                    <h3>'$row[0]'</h3>
                    <input type='radio' name='question-1-answers' id='question-1-answers-A' value='A' />
                    <label for='question-1-answers-A'>A) Computer Styled Sections </label>
                    <input type='radio' name='question-1-answers' id='question-1-answers-B' value='B' />
                    <label for='question-1-answers-B'>B) Cascading Style Sheets</label>
                </li>
                <li>
                    <h3>'$row[0]'</h3>
                    <input type='radio' name='question-2-answers' id='question-2-answers-A' value='A' />
                    <label for='question-2-answers-A'>A) 2001</label>
                    <input type='radio' name='question-2-answers' id='question-2-answers-B' value='B' />
                    <label for='question-2-answers-B'>B) 1998</label>
                </li>
            </ol>
            <input type='submit' value='Submit Quiz' />
        </form>";
     echo $quiz;
    }
    /* free result set */
    $result->close();
}

1 个答案:

答案 0 :(得分:2)

作为指导,我可以建议这样的事情吗?

<?php
    if ( $result = $mysqli->query( "SELECT question, answerA,answerB FROM questions WHERE type='$type'" ) ) {

        /* rather than string concatenation use an array */
        $html=array();

        $html[]="
        <form action='' method='post' id='quiz'>
            <ol>";

        $counter=1;

        /* add a question and associated answers for each row */
        while( $row = $result->fetch_array() ) {

            /* Fetch recordset fields into vars */
            $question=$row['question'];
            $answerA=$row['answerA'];
            $answerB=$row['answerB'];

            $html[]="
                <h3>Question {$counter}: {$question}</h3>
                <li>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answers-A' value='A' />
                    <label for='question-{$counter}-answers-A'>A) {$answerA} </label>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answers-B' value='B' />
                    <label for='question-{$counter}-answers-B'>B) {$answerB} </label>
                </li>";

            $counter++;
        }

        $html[]="
            </ol>
            <input type='submit' value='Submit Quiz' />
        </form>";

        echo implode( PHP_EOL, $html );
        $result->close();
    }

?>