With the code below I am trying to retrieve the list of possible answers by first finishing a question's Option Type
and then removing the correct answers (Answer
field`) from the list of answers.
My question though is that I just need a little help finishing the code off to be able to do this. I am getting an notices in the $row
variable where I know I have not called up on it before the if statement to refer to it but my question on that is what is $row
variable suppose to be set as or do I need to call $row
something else?
Example notice received:
Notice: Undefined variable: row in ... on line ...
Notice: Trying to get property of non-object in ... on line ...
Another is issue if you look at code at the very bottom, when I try to display the incorrect answers <?php
echo $incorrect_ans[$key];
?>
It keeps displaying the word Array
. Am I calling the array incorrectly? I want it to display the incorrect answers recieved.
Below is the full code
$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>
If you want to see the database tables to see what is in each table then have a look below:
DB Table Structure:
Session Table (aka Exam Table)
SessionId(auto) SessionName
137 XULWQ
Question Table:
SessionId QuestionId QuestionContent QuestionNo QuestionMarks OptionId
137 1 Name 2 Things 1 5 5
137 2 Name 3 Things 2 5 2
Option_Table Table:
OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F
5 A-G
6 A-H
Answer Table:
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
UPDATE:
Only issue now is the layout of the incorrect answers, I want it to display each incorrect answer in its own row per question:
So lets say below are the correct and incorrect answers for each question:
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
Below shows the current layout and the way it should be laid out:
The code for the current output is this:
<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>
Problem with
$row
is pretty simple - there is no variable$row
defined. Instead of fetching rows you're binding variables to result values, so just replace$row->OptionType
with bound variable$dbOptionType
and similar$row->Answer
with$dbAnswer
. To fetch rows you should rather do it this way:In example code I don't see also
$incorrect_ans
variable defined, maybe you're doing it somewhere else but if not then add$incorrect_ans = array()
for example there where you're defining other lists (below// This will hold the search results
line).Try doing
print_r
on$incorrect_ans
then you'll see structure of result array, you've got there list of arrays and thats whyecho $incorrect_ans[$key];
is resulting inArray
. You can doecho implode(',', $incorrect_ans[$key]);
and you'll get string with values separated by,
character.I'd suggest to define
$specialOptionTypes
before while loop, you're not changing that array inside a loop so I suppose there is no reason to recreate it on every iteration.I noticed also that you're setting
name
attribute to<td>
which does not have such attribute (as far as I know) - maybe you wanted to use some<input>
?