How to get the wrong answers from the list of possible answers

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:

enter image description here

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> 
+1
source share
1 answer

The problem with $row pretty simple - there is no $row variable. Instead of fetching rows, you bind the variables to the result values, so just replace $row->OptionType with the associated variable $dbOptionType and a similar $row->Answer with $dbAnswer . To get the strings you have to do it like this:

 $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_object()) { // ... } 

In the code example, I donโ€™t see the $incorrect_ans variable either, maybe you do it somewhere else, but if not, then add $incorrect_ans = array() , for example, where you define other lists (below the line // This will hold the search results ).

Try making print_r to $incorrect_ans , then you will see the structure of the result array, you have a list of arrays, and that is why echo $incorrect_ans[$key]; leads to Array . You can do echo implode(',', $incorrect_ans[$key]); , and you will get a string with values โ€‹โ€‹separated by,.

I would suggest defining $specialOptionTypes before the while loop, you do not modify this array inside the loop, so I suppose there is no reason to recreate it at each iteration.

I also noticed that you are setting the name attribute to <td> , which does not have such an attribute (as far as I know) - perhaps you wanted to use some <input> ?

 <?php // ......... $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(); $incorrect_ans = 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('Y', 'N'), 'True or False' => array('T', 'F'), ); while ($stmt->fetch()) { // Do this for each row: if (array_key_exists($dbOptionType, $specialOptionTypes)) { $options = $specialOptionTypes[$dbOptionType]; } else if (preg_match('/^([AZ])-([AZ])$/', $dbOptionType, $match)) { $options = range($match[1], $match[2]); } else { // issue warning about unrecognized option type $options = array(); } $right = str_split($dbAnswer); // 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; } // ......... for ($key = 0, $cnt = count($incorrect_ans) ; $key < $cnt ; ++$key) : ?> <td class="answertd"> <?php echo implode(',', $incorrect_ans[$key]); ?> </td> <?php endfor; ?> 
+2
source

All Articles