如何从php mysql中的数据库中检索问题,答案

时间:2015-05-19 12:09:42

标签: php mysql

我在php中进行测验并遇到一些问题。所以我想从数据库中检索问题,答案和答案点。 例如,我有10个问题,当用户标记我想要计算标记的单选按钮的点的所有问题。我想根据数据库值动态地做它。我不想静态检索10个单选按钮的值并总结它。

<div class="test-data">

<form method="POST" action="index.php">

<?php foreach($test_data as $id_question => $item): ?>

<div class="question" data-id="<?=$id_question?>" id="question-<?=$id_question?>">

<?php foreach($item as $point => $answer): ?>

<?php if(!$point): ?>
    <p class="q"><?=$answer?></p>
        <?php else: ?>
            <p class="a">
                <input type="radio" id="answer-<?=$id_answer?>" name="question-<?=$id_question?>" value="<?=$point?>" /><?=$answer?>
            </p>
<?php endif; ?>

<?php endforeach; ?>

</div>

<?php endforeach; ?>

            <input type="submit" name="finish_test" id="finish_test" value="Finish" />

</form>

</div>

<?php endif; ?>

这是我的功能,它可以检索问题,答案和要点

function get_test_data(){
$query = "SELECT q.question, q.quizz_id, a.id, a.answer, a.question_id, a.point
                    FROM questions q
                    LEFT JOIN answers a
                        ON q.id = a.question_id";
$res = mysql_query($query);
$data = null;
while($row = mysql_fetch_assoc($res)){
    if(!$row['question_id']) return false;
    $data[$row['question_id']]['0'] = $row['question'];
    $data[$row['question_id']][$row['point']] = $row['answer'];
}
return $data;                      

}

0 个答案:

没有答案