Using the code below, I try to get a list of possible answers by first completing the Option Type question and then removing the correct answers ( Answer field`) from the list of answers.
My question is that I just need a little help completing the code to do this. I get notifications in the $row variable, where I know that I did not call it before the if statement to refer to it, but my question is that this $row variable should presumably be set as or do I need to call $row something else?
Notification Accepted:
Note: Undefined variable: row in ... on line ...
Note: attempt to get non-object property in ... on line ...
Another thing is if you look at the code at the very bottom when I try to display incorrect answers <?php echo $incorrect_ans[$key]; ?> <?php echo $incorrect_ans[$key]; ?> It continues to display the word Array . Am I calling an array incorrectly? I want it to display incorrect answers.
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( '/^([AZ])-([AZ])$/', $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 each table has, 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 AC 2 AD 3 AE 4 AF 5 AG 6 AH
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 the question now is the layout of the wrong answers, I want it to display each incorrect answer in its line to the question:
So, let's say below the right and wrong answers for each question:
Question Number: 1 Correct Answer(s) B Incorrect Answers ACD Question Number: 2 Correct Answer(s) AC Incorrect Answers BD Question Number: 3 Correct Answer(s) D Incorrect Answers ABC
Below is the current layout and how to place it:

Code for current output:
<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>