我的输出没有Total Marks
正确插入数据,似乎缺少问题 1 的总分,这是因为每个问题的分数如下所示:
Question 1: 3
Question 2: 5
Question 3: 2
现在表格包含不正确的答案,所以表格看起来像我发现的这个小提琴http://phpfiddle.org/main/code/7j1-we2,如果您查看结果,您会发现问题 1 和 2 都包含问题2 标记,并且 queston 3 包含它的标记。但是我怎样才能得到正确的标记来显示,为什么它缺少第一个问号?
更新 1:
$query = "SELECT q.QuestionNo, an.Answer, q.TotalMarks, o.OptionType
FROM
Question q INNER JOIN Answer an ON q.QuestionID = an.QuestionID
INNER JOIN Option_Table o ON o.OptionID = q.OptionID
ORDER BY q.QuestionId, an.Answer
";
// prepare query
$stmt=$mysqli->prepare($query);
// execute query
$stmt->execute();
// This will hold the search results
$searchQuestionNo = array();
$totalMarks = array();
$incorrect_ans = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbQuestionNo, $dbAnswer, $dbTotalMarks $dbOptionType);
$specialOptionTypes = array('Yes or No' => array( 'Yes', 'No' ),'True or False' => array( 'True', 'False' ));
while ($stmt->fetch()) {
// Do this for each row:
if ( array_key_exists( $dbOptionType, $specialOptionTypes ) ) {
$options = $specialOptionTypes[$dbOptionType];
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $dbOptionType, $match ) ) {
$options = range( $match[1], $match[2] );
} else {
// issue warning about unrecognized option type
$options = array();
}
$right = str_split( $dbAnswer );
$wrong = array_diff( $options, $right );
$searchQuestionNo[] = $dbQuestionNo;
$totalMarks[] = $dbQuestionMarks;
}
以上是检索错误答案的代码。对于每个问题会发生什么,它会检索每个问题$dbOptionType
并显示可能的答案列表。例如,如果问题 1$dbOptionType
是A - D
,那么Answers
它显示的列表是A, B, C, D
。$dbQuestionNo
上面的代码还使用and检索每个问题的编号和总分$TotalMarks
。
但是,如果一个问题有多个答案,它会显示两组答案并从一组答案中删除一个。这是因为在数据库中,要获得具有多个答案和单个答案的问题的正确答案,如下所示:
QuestionNo Answer TotalMarks OptionType
1 B 3 A-D
2 A 5 A-D
2 C 5 A-D
3 D 2 A-D
更新 2:
对于问题 2,因为它是多个答案,这就是为什么它在数组中显示两组数据。因为如果一个问题中有多个答案,它删除正确答案的方式如下:
Question 2: Answers: A, B, C, D Remove Correct Answer: A Incorrect Answers: B, C, D
Question 2: Answers: A, B, C, D Remove Correct Answer: C Incorrect Answers: A, B, D
如果问题有多个正确答案,则删除正确答案的方式如上所述。这是:
- 显示可能的答案
- 删除 1 个正确答案
A
- 显示不正确的答案
B, C, D
要删除第二个正确答案,它会重复该过程:
- 显示可能的答案
- 删除 1 个正确答案
C
- 显示不正确的答案
A, C, D
(我们知道A
不正确,但因为它一次只删除一个正确的答案并显示所有可能的答案,所以它A
在第二组中表现得好像不正确但在第一组中正确)
更新 3:
该错误是由于您使用 for() 在 $ques_ans 上迭代的数组在键中存在间隙。
var_dump($ques_ans) gives us:
array(3) {
... skipped for brevity
[2]=>
array(2) {
[0]=>
string(1) "B"
[2]=>
string(1) "D"
}
... skipped for brevity
}
没有键为1的元素。这是因为我在第 49 行使用的函数 array_intersect 保留了键。
为了快速修复代码以使其正常工作,我在第 51 行添加了 array_values():
$ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans
反对 ques no 作为键。
但我仍然得到奇怪的差距$keys
。
这是代码:
下面是代码:
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, q.OptionId, o.OptionType
FROM
Question q INNER JOIN Answer an ON q.QuestionID = an.QuestionID
INNER JOIN Option_Table o ON o.OptionID = q.OptionID
INNER JOIN Session s ON s.Sessionid = q.Sessionid
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $assessment);
// execute query
$stmt->execute();
// This will hold the search results
$searchQuestionNo = array();
$searchQuestionContent = array();
$totalMarks = array();
$searchAnswerId = array();
$incorrect_ans = array();
$searchMarks = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);
$specialOptionTypes = array('Yes or No' => array( 'Yes', 'No' ),'True or False' => array( 'True', 'False' ));
while ($stmt->fetch()) {
// Do this for each row:
if ( array_key_exists( $dbOptionType, $specialOptionTypes ) ) {
$options = $specialOptionTypes[$dbOptionType];
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $dbOptionType, $match ) ) {
$options = range( $match[1], $match[2] );
} else {
// issue warning about unrecognized option type
$options = array();
}
$right = str_split( $dbAnswer );
$wrong = array_diff( $options, $right );
$searchQuestionNo[] = $dbQuestionNo;
$searchQuestionContent[] = $dbQuestionContent;
$incorrect_ans[] = $wrong;
$searchAnswerId[] = $dbAnswerId;
$totalMarks[] = $dbQuestionMarks;
$searchMarks[] = $dbQuestionMarks;
}
?>
</head>
<body>
<?php
$ques_ans = array(); //to store incorrect answers against ques no.
$q_occ_count = array_count_values($searchQuestionNo);
foreach ($searchQuestionNo as $key => $questionNo) {
if (!array_key_exists($questionNo, $ques_ans)) {
if ($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans
{
$ques_ans[$questionNo] = $incorrect_ans[$key]; //store the array of incorrect ans against the ques no as key
} else //if a ques has more than 1 correct ans
{
//find the intersection of incorrect_ans arrays for this ques
$q_keys = array_keys($searchQuestionNo, $questionNo);
$q_incorrect_ans = $incorrect_ans[$q_keys[0]];
foreach ($q_keys as $q_key) {
$q_incorrect_ans = array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]);
}
$ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans against the ques no as key
}
}
}
var_dump($ques_ans);
?>
<table id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='incorrectanswerth'>Incorrect Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<?php
foreach ($ques_ans as $questionNo => $inc_ans) {
$q_row_span = count($inc_ans);
$row_count = 0;
?>
<tr class="questiontd">
<td class="questionnumtd q<?php
echo $questionNo;
?>_qnum" rowspan="<?php
echo $q_row_span;
?>"><?php
echo $questionNo;
?>
<input type="hidden" name="numQuestion" value="<?php
echo $questionNo;
?>" />
<input type="hidden" name="q<?php
echo $questionNo;
?>_ans_org" class="q<?php
echo $questionNo;
?>_ans_org" value="<?php
echo $searchMarks[array_search($questionNo, $searchQuestionNo)];
?>">
<input type="hidden" name="q<?php
echo $questionNo;
?>_ans" class="q<?php
echo $questionNo;
?>_ans" value="<?php
echo $searchMarks[array_search($questionNo, $searchQuestionNo)];
?>">
</td>
<td class="questioncontenttd" rowspan="<?php
echo $q_row_span;
?>"><?php
echo $searchQuestionContent[array_search($questionNo, $searchQuestionNo)];
?> </td>
<td class="answertd"><?php
echo $inc_ans[$row_count];
?>
<input type="hidden" id="hiddenincorrect" name="incorrect[]" value="<?php
echo $inc_ans[$row_count];
?>">
</td>
<td class="answermarkstd">
<input class="individualMarks q<?php
echo $questionNo;
?>_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php
echo $questionNo;
?>" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="totalmarkstd" rowspan="<?php
echo $q_row_span;
?>"><?php
echo $totalMarks[array_search($questionNo, $searchQuestionNo)];
?></td>
<td class="noofmarkstd q<?php
echo $questionNo;
?>_ans_text" q_group="1" rowspan="<?php
$q_row_span;
?>"><?php
echo "<strong>" . $searchMarks[array_search($questionNo, $searchQuestionNo)] . "</strong>";
?></td>
</tr>
<?php
//remaining incorrect answers in separate row (if any) follows here
if ($row_count < $q_row_span - 1) {
for ($i = ($row_count + 1); $i < $q_row_span; $i++) {
?>
<tr>
<td class="answertd"><?php
echo $inc_ans[$i];
?>
<input type="hidden" id="hiddenincorrect" name="incorrect[]" value="<?php
echo $inc_ans[$i];
?>">
</td>
<td class="answermarkstd">
<input class="individualMarks q<?php
echo $questionNo;
?>_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php
echo $questionNo;
?>" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<?php
}
}
}
?>
</tbody>
</table>
<p>
<input type='hidden' id='num_groups' name='num_groups' value='<?php
echo $questionNo;
?>'>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
</p>
</form>
截屏: