Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display incorrect answers using html/php/mysqli
    primarykey
    data
    text
    <p>I have a query below where it displays the correct answer per question in an exam: </p> <pre><code>&lt;? $query = "SELECT q.SessionId, q.QuestionId an.Answer FROM Session s INNER JOIN Question q ON s.SessionId = q.SessionId JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId WHERE s.SessionName = "XULWQ" 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 $searchQuestionId = array(); $searchAnswer = array(); // Fetch the results into an array // get result and assign variables (prefix with db) $stmt-&gt;bind_result($dbSessionId, $dbQuestionId, $dbAnswer); while ($stmt-&gt;fetch()) { $searchQuestionId[] = $dbQuestionId; $searchAnswer[] = $dbAnswer; } ?&gt; </code></pre> <p>Below is the result is outputs from the query:</p> <pre><code>SessionId QuestionId Answer 137 1 B 137 1 D 137 1 F 137 2 A 137 2 C </code></pre> <p>Now I have stored the data above in a php/html table using code below:</p> <pre><code>&lt;table border='1' id='markstbl'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class='questionth'&gt;Question No.&lt;/th&gt; &lt;th class='answerth'&gt;Answer&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php $row_span = array_count_values($searchQuestionId); $prev_ques = ''; foreach($searchQuestionId as $key=&gt;$questionId){ ?&gt; &lt;tr class="questiontd"&gt; &lt;?php if($questionId != $prev_ques){ ?&gt; &lt;td class="questionnumtd q&lt;?php echo$questionId?&gt;_qnum" name="numQuestion" rowspan="&lt;?php echo$row_span[$questionId]?&gt;"&gt; &lt;?php echo$questionId?&gt; &lt;/td&gt; &lt;?php } ?&gt; &lt;td class="answertd" name="answers[]"&gt;&lt;?php echo$searchAnswer[$key]?&gt;&lt;input type='hidden' id='hidanswerid' name='answersId[]' value='&lt;?php echo$searchAnswerId[$key]?&gt;'&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php $prev_ques = $questionId; } ?&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>The output of the html/php table:</p> <p><img src="https://i.stack.imgur.com/hOhAS.png" alt="enter image description here"></p> <p>So you can see I outputted a table where it contains the correct answers per question in that exam. Great. But now I want to create another page which is similar but this time except displaying the correct answers per question, I want it to display the incorrect answers per question.</p> <p>I am not sure what is best way to do it but my plan I believe is to first retrieve the option_type for each question (option type is the number of options to select an answer from):</p> <p>Query:</p> <pre><code> $query="SELECT q.SessionId, q.QuestionId, o.OptionId FROM SESSION s INNER JOIN Question q ON s.SessionId = q.SessionId JOIN Option_Table o ON q.OptionId = o.OptionId WHERE s.SessionName = "XULWQ" ORDER BY q.QuestionId"; </code></pre> <p>Result:</p> <pre><code>SessionId QuestionId OptionId 137 1 5 137 2 2 </code></pre> <p>Then use a case statement in php to display the letters for each case (need help coding this):</p> <p>E.g </p> <pre><code>case 1, OptionId = 1, letters = A B C case 2, OptionId = 2, letters = A B C D case 3, OptionId = 3, letters = A B C D E ... //continue going down case 26, OptionId = 26, letters = A B C D E ... Z case 27, OptionId = 27, letters = True False (Options True or False) case 28, OptionId = 28, letters = Yes or No (Options Yes or No) </code></pre> <p>Then some how remove the correct answers from the letters (maybe using query but I am not sure) so the html/php table contains all the incorrect answers rather than the correct answers in the table</p> <p>So my question is how can it be coded in order to display the incorrect answers rather than the correct answer in the html/php table?</p> <p>The output of the html/php table from the example above should look like this:</p> <p><img src="https://i.stack.imgur.com/B5KcX.png" alt="enter image description here"></p> <p>Comment: You can exchange the answers among questions while printing. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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