基本上是这样的情景:
我正在尝试从数据库生成测试。每个测试都有很多问题(项目),每个项目都属于某种类型(多项选择等),每个问题都附有多个答案。
(我不是在找人告诉我,我的一切都错了)
我目前循环查询结果并打印出每个问题。在该循环中,我为每个问题执行一个函数(present_question)。在此功能中有一个开关盒,用于识别物品类型。在每种情况下,我都会运行另一个循环来打印出与该问题相关的所有答案。
我想要做的是将每个问题插入一个独特的<div>
,以便在每个问题之间切换显示等,因为他们为每个问题选择了一个按钮,所以1,2,3,4等等。 / p>
但是,看起来我正在努力解决这个问题。我的问题都打印出来,但是看起来它们并不在个人<divs>
内,因为我尝试通过改变类的一些属性来测试它并且它什么都不做。 :(
以下是涵盖整个程序的主要代码:
主循环:
//loop through every question present in query results and run function to present the different question structures
while ($qs = mysql_fetch_assoc($get_questions))
{
$type = $qs['item_type'];
$item_id = $qs['item_id'];
$question = $qs['question_text'];
echo "<div class='q_center' id='q_$q_num'>"; // insert each question into its own div
echo "<h2>Question No.$q_num </h2><br>";
echo "$question <br>"; // print out actual question
present_question($item_id, $type);
$q_num ++;
}
present_question函数
function present_question($ID, $type){
// grab all the answers attached to the question
$get_answers = mysql_query("SELECT * FROM answers_tb WHERE item_id='$ID'");
echo "<form>";
// switch for different structured questions. (switched by question type)
switch ($type)
{
case "1":
multi_choice($get_answers); // just working on this type at the moment
break;
case "2":
echo "this type hasnt been done yet";
break;
}
echo "</div>";
echo "</form>";
return;
}
多选类型的功能
function multi_choice($get_answers){
while ($answers = mysql_fetch_assoc($get_answers))
{
$as = $answers['text_value'];
echo "<input type='radio' name='1' value='$as'>$as<hr />";
}
return;
}
希望你们能帮助我。
我是初学者,所以如果我的代码令人厌恶,我会道歉:D
感谢,
答案 0 :(得分:0)
粗略版本(未经测试)
$i = 0; //declares the counter $i
//loop through every question present in query results and run function to present the different question structures
while ($qs = mysql_fetch_assoc($get_questions))
{
$type = $qs['item_type'];
$item_id = $qs['item_id'];
$question = $qs['question_text'];
$i++; // adds 1 to $i
echo "<div class='q_center' id='q_$q_num'>"; // insert each question into its own div
echo "<h2>Question No.$q_num </h2><br>";
echo "<div class=question" .$i. ">"; //appends $i to the div name creating question[$i]
echo "$question <br>"; // print out actual question
echo "</div>";
present_question($item_id, $type);
$q_num ++;
}
应该将div命名为question1,question2等