我刚开始使用php,并希望做一个有趣的项目来改善它。我创建了一个以问题开头的文本文件,然后有答案选择,然后有答案索引:
What does charmander evolve to?#Charmeleon:charizard:squirtle#0
Who is the main character in Pokemon?#Misty:Ash:Brock#1
如何根据问题的数量生成单选按钮?
<?php
$quizStuff = file("quiz1.txt");
foreach ($quizStuff as $questions) {
$questionParse = explode("#", $question);
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
foreach ($answerChoices as $answerChoice) {
# create radio button and print answer choice next to it
}
}
?>
由于单选按钮是一种表单形式,我认为在问题结束时我会有一个提交按钮,用户按下提交后会弹出下一个问题。一般设计输入也很棒!
答案 0 :(得分:0)
我不习惯将文本文件作为数据库,但这是我看到的解决方案。我放了一个计数器来确定哪个问题是包含选择的地方。
$ctr = 1;
foreach ($quizStuff as $questions) {
$questionParse = explode("#", $question);
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' id='".$answerChoice.$ctr."' name='question$ctr' value='$answerChoice'> <label for='".$answerChoice.$ctr."'>".$answerChoice."</label><br>"
}
}