Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve incorrect answers from a list of possible answers
    primarykey
    data
    text
    <p>With the code below I am trying to retrieve the list of possible answers by first finishing a question's <code>Option Type</code> and then removing the correct answers (<code>Answer</code> field`) from the list of answers. </p> <p>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 <code>$row</code> 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 <code>$row</code> variable suppose to be set as or do I need to call <code>$row</code> something else?</p> <p>Example notice received:</p> <blockquote> <p>Notice: Undefined variable: row in ... on line ... </p> <p>Notice: Trying to get property of non-object in ... on line ...</p> </blockquote> <p>Another is issue if you look at code at the very bottom, when I try to display the incorrect answers <code>&lt;?php echo $incorrect_ans[$key]; ?&gt;</code> It keeps displaying the word <code>Array</code>. Am I calling the array incorrectly? I want it to display the incorrect answers recieved.</p> <p>Below is the full code</p> <pre><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-&gt;prepare($query); // You only need to call bind_param once $stmt-&gt;bind_param("s", $assessment); // execute query $stmt-&gt;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-&gt;bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType); while ($stmt-&gt;fetch()) { $specialOptionTypes = array( 'Yes or No' =&gt; array( 'Y', 'N' ), 'True or False' =&gt; array( 'T', 'F' ), ); // Do this for each row: if ( array_key_exists( $row-&gt;OptionType, $specialOptionTypes ) ) { $options = $specialOptionTypes[ $row-&gt;OptionType ]; } else if ( preg_match( '/^([A-Z])-([A-Z])$/', $row-&gt;OptionType, $match ) ) { $options = range( $match[1], $match[2] ); } else { // issue warning about unrecognized option type $options = array(); } $right = str_split( $row-&gt;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 &lt;td class="answertd" name="incorrectanswers[]"&gt;&lt;?php echo $incorrect_ans[$key]; ?&gt;&lt;/td&gt; </code></pre> <p>If you want to see the database tables to see what is in each table then have a look below:</p> <p><strong>DB Table Structure:</strong></p> <p>Session Table (aka Exam Table)</p> <pre><code>SessionId(auto) SessionName 137 XULWQ </code></pre> <p>Question Table:</p> <pre><code>SessionId QuestionId QuestionContent QuestionNo QuestionMarks OptionId 137 1 Name 2 Things 1 5 5 137 2 Name 3 Things 2 5 2 </code></pre> <p>Option_Table Table:</p> <pre><code>OptionId OptionType 1 A-C 2 A-D 3 A-E 4 A-F 5 A-G 6 A-H </code></pre> <p>Answer Table:</p> <pre><code> 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 </code></pre> <p><strong>UPDATE:</strong></p> <p>Only issue now is the layout of the incorrect answers, I want it to display each incorrect answer in its own row per question:</p> <p>So lets say below are the correct and incorrect answers for each question:</p> <pre><code>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 </code></pre> <p>Below shows the current layout and the way it should be laid out:</p> <p><img src="https://i.stack.imgur.com/rKKZn.png" alt="enter image description here"></p> <p>The code for the current output is this:</p> <pre><code>&lt;table border='1' id='penaltytbl'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class='questionth'&gt;Question No.&lt;/th&gt; &lt;th class='answerth'&gt;Incorrect Answer&lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php $row_span = array_count_values($searchQuestionNo); $prev_ques = ''; foreach($searchQuestionNo as $key=&gt;$questionNo){ ?&gt; &lt;tr class="questiontd"&gt; &lt;?php if($questionNo != $prev_ques){ ?&gt; &lt;td class="questionnumtd q&lt;?php echo$questionNo?&gt;_qnum" rowspan="&lt;?php echo$row_span[$questionNo]?&gt;"&gt; &lt;?php echo$questionNo?&gt;&lt;input type="hidden" name="numQuestion" value="&lt;?php echo$questionNo?&gt;" /&gt; &lt;/td&gt; &lt;?php } ?&gt; &lt;td class="answertd"&gt;&lt;?php echo implode(',', $incorrect_ans[$key]);?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php $prev_ques = $questionNo; } ?&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload