我有一个使用数组来存储问题的测验:
$array=array(
'1'=> array('1','What does everyone know when they see it?','quest1','Good publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
'2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);
然后我使用这个循环显示问题:
foreach($array as $quiz)
{
echo
'<div class="question">
<div class="questionList">
<h4>' . $quiz[0] . '. <strong>' . $quiz[1] . '</strong></h4>
<ul>
<li><input type="radio" name="' . $quiz[2] . '" value="1"> '. $quiz[3] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="2"> '. $quiz[4] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="3"> '. $quiz[5] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="4"> '. $quiz[6] . '</li>
</ul>
</div>
<img src="'. $quiz[7] . '" class="questionImg01" />
<div class="clr"></div>
</div>
';
}
}
如果有人第一次没有回答所有问题,我会将每个问题的值存储在MySQL中,如果没有答案则存储为“0”。我想检查一下用户已经回答的单选按钮,但是无法确定如何将测验数组和MySQL结果结合起来。
结果的表结构是:
quiz_id |用户名| quest1 | quest2 | quest3 | quest4 | quest5 | quest6 | quest7 | quest8 | quest9 | quest10 |完整
其中quiz_id是该测验的参考编号,而完成是一个标记,如果用户已回答所有问题则记录。我正在检索这样的结果:
$results = mysql_query("SELECT * FROM quiz_results_baseline WHERE username = ('$username')");
我有没有办法将这两者结合起来?
我也遇到了无法使此查询包含WHERE语句的问题 - 我想将所有测验结果存储在一个表中,然后使用quiz_id引用每个用户的测验结果:
mysql_query("INSERT INTO quiz_results_baseline (quest1,quest2,quest3,quest4,quest5,quest6,quest7,quest8,quest9,quest10,username,quiz_id,complete) VALUES $queryData ON DUPLICATE KEY UPDATE quest1=VALUES(quest1),quest2=VALUES(quest2),quest3=VALUES(quest3),quest4=VALUES(quest4),quest5=VALUES(quest5),quest6=VALUES(quest6),quest7=VALUES(quest7),quest8=VALUES(quest8),quest9=VALUES(quest9),quest10=VALUES(quest10),complete=VALUES(complete)");
任何帮助将不胜感激!
答案 0 :(得分:0)
为了让您完成一半完成的测验结果以及测验,请尝试以下代码。
$questions=array(
'1'=> array('1','What does everyone know when they see it?','quest1','Good publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
'2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);
// do your query for the row of answers
// $answers = mysql_fetch_assoc(...);
// for testing
$answers = array(
'quest1' => '1',
'quest2' => '4');
for($i = 1; $i <= count($questions); $i++)
{
$quiz = $questions[$i];
$answer = $answers[$quiz[2]];
echo
'<div class="question">
<div class="questionList">
<h4>' . $quiz[0] . '. <strong>' . $quiz[1] . '</strong></h4>
<ul>
<li><input type="radio" name="' . $quiz[2] . '" value="1" ' . (($answer == '1') ? 'checked' : '') . '> '. $quiz[3] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="2" ' . (($answer == '2') ? 'checked' : '') . '> '. $quiz[4] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="3" ' . (($answer == '3') ? 'checked' : '') . '> '. $quiz[5] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="4" ' . (($answer == '4') ? 'checked' : '') . '> '. $quiz[6] . '</li>
</ul>
</div>
<img src="'. $quiz[7] . '" class="questionImg01" />
<div class="clr"></div>
</div>
';
}
您必须执行查询以获取$answers
数组,但我输入了一个示例,以便您了解如何使用结果。
我运行了代码,结果如下:
希望有所帮助!