使用下面的代码,我试图通过首先完成一个问题Option Type
然后从答案列表中删除正确答案(Answer
字段)来检索可能的答案列表。
我的问题是,我只需要一点帮助来完成代码就可以做到这一点。我在$row
变量中收到通知,我知道我没有在 if 语句之前调用它来引用它,但我的问题是什么是$row
变量假设设置为或者我需要调用$row
其他东西?
收到的示例通知:
注意:未定义的变量:row in ... on line ...
注意:试图在线获取...中非对象的属性...
另一个问题是,如果您查看最底部的代码,当我尝试显示不正确的答案时<?php
echo $incorrect_ans[$key];
?>
,它会一直显示单词Array
。我是否错误地调用了数组?我希望它显示收到的错误答案。
以下是完整代码
$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();
$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);
while ($stmt->fetch()) {
$specialOptionTypes = array(
'Yes or No' => array( 'Y', 'N' ),
'True or False' => array( 'T', 'F' ),
);
// Do this for each row:
if ( array_key_exists( $row->OptionType, $specialOptionTypes ) ) {
$options = $specialOptionTypes[ $row->OptionType ];
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $row->OptionType, $match ) ) {
$options = range( $match[1], $match[2] );
} else {
// issue warning about unrecognized option type
$options = array();
}
$right = str_split( $row->Answer ); // or explode() on a delimiter, if any
$wrong = array_diff( $options, $right );
$searchQuestionNo[] = $dbQuestionNo;
$searchQuestionContent[] = $dbQuestionContent;
$incorrect_ans[] = $wrong;
$searchAnswerId[] = $dbAnswerId;
$totalMarks[] = $dbQuestionMarks;
$searchMarks[] = $dbQuestionMarks;
}
....
//table row
<td class="answertd" name="incorrectanswers[]"><?php
echo $incorrect_ans[$key];
?></td>
如果您想查看数据库表以查看每个表中的内容,请查看以下内容:
数据库表结构:
会话表(又名考试表)
SessionId(auto) SessionName
137 XULWQ
问题表:
SessionId QuestionId QuestionContent QuestionNo QuestionMarks OptionId
137 1 Name 2 Things 1 5 5
137 2 Name 3 Things 2 5 2
Option_Table 表:
OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F
5 A-G
6 A-H
答案表:
AnswerId(auto) SessionId QuestionId Answer
200 137 1 B
201 137 1 F
202 137 2 D
203 137 2 A
204 137 2 C
更新:
现在唯一的问题是错误答案的布局,我希望它在每个问题自己的行中显示每个错误答案:
所以让我们说下面是每个问题的正确和错误答案:
Question Number: 1 Correct Answer(s) B Incorrect Answers A C D
Question Number: 2 Correct Answer(s) A C Incorrect Answers B D
Question Number: 3 Correct Answer(s) D Incorrect Answers A B C
下面显示了当前的布局和它的布局方式:
当前输出的代码是这样的:
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th></tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionNo);
$prev_ques = '';
foreach($searchQuestionNo as $key=>$questionNo){
?>
<tr class="questiontd">
<?php
if($questionNo != $prev_ques){
?>
<td class="questionnumtd q<?php echo$questionNo?>_qnum" rowspan="<?php echo$row_span[$questionNo]?>">
<?php echo$questionNo?><input type="hidden" name="numQuestion" value="<?php echo$questionNo?>" />
</td>
<?php
}
?>
<td class="answertd"><?php echo implode(',', $incorrect_ans[$key]);?></td>
</tr>
<?php
$prev_ques = $questionNo;
}
?>
</tbody>
</table>