我的输出没有正确插入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
和$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
如果问题有多个正确答案,则删除正确答案的方式如上所述。它是:
A
B, C, D
要删除第二个正确答案,请重复此过程:
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
反对问题不作为关键。
但我仍然在$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>
截图:
答案 0 :(得分:1)
问题在于您检索总分数的方式。您使用$totalMarks
作为索引访问$questionNo
,但应该使用该问题的实际数组索引。
工作代码:
<td class="totalmarkstd" rowspan="<?php echo $q_row_span?>">
<?php echo $totalMarks[array_search($questionNo, $searchQuestionNo)]?>
</td>
更新1:
$ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans against the ques no as key
更新2:
使用array_values。
foreach($ques_ans as $questionNo => $inc_ans)
{
$inc_ans = array_values($inc_ans);
更新了phpFiddle:http://phpfiddle.org/main/code/get-rps