Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect data displayed in html table
    text
    copied!<p>My output does not insert the data for <code>Total Marks</code> correctly, it seems like it is missing the total marks for question 1, This is because the marks for each question goes like this:</p> <pre><code>Question 1: 3 Question 2: 5 Question 3: 2 </code></pre> <p>Now the table contains incorrect answers so the table looks like this fiddle which I have found <a href="http://phpfiddle.org/main/code/7j1-we2" rel="nofollow noreferrer">http://phpfiddle.org/main/code/7j1-we2</a>, if you look at the results, you realize that both question 1 and 2 contain question 2 marks and that queston 3 contains its marks. But how can I get the correct marks to be display, why is it missing the first question marks?</p> <p><strong>UPDATE 1:</strong></p> <pre><code>$query = "SELECT q.QuestionNo, an.Answer, q.TotalMarks, o.OptionType FROM Question q INNER JOIN Answer an ON q.QuestionID = an.QuestionID INNER JOIN Option_Table o ON o.OptionID = q.OptionID ORDER BY q.QuestionId, an.Answer "; // prepare query $stmt=$mysqli-&gt;prepare($query); // execute query $stmt-&gt;execute(); // This will hold the search results $searchQuestionNo = array(); $totalMarks = array(); $incorrect_ans = array(); // Fetch the results into an array // get result and assign variables (prefix with db) $stmt-&gt;bind_result($dbQuestionNo, $dbAnswer, $dbTotalMarks $dbOptionType); $specialOptionTypes = array('Yes or No' =&gt; array( 'Yes', 'No' ),'True or False' =&gt; array( 'True', 'False' )); while ($stmt-&gt;fetch()) { // Do this for each row: if ( array_key_exists( $dbOptionType, $specialOptionTypes ) ) { $options = $specialOptionTypes[$dbOptionType]; } else if ( preg_match( '/^([A-Z])-([A-Z])$/', $dbOptionType, $match ) ) { $options = range( $match[1], $match[2] ); } else { // issue warning about unrecognized option type $options = array(); } $right = str_split( $dbAnswer ); $wrong = array_diff( $options, $right ); $searchQuestionNo[] = $dbQuestionNo; $totalMarks[] = $dbQuestionMarks; } </code></pre> <p>Above is the code which retrieves the wrong answers. What happens is for each question, it retireves each questions <code>$dbOptionType</code> and display the list of possible answers. So for example if question 1's <code>$dbOptionType</code> is <code>A - D</code>, then the list of <code>Answers</code> it displays is <code>A, B, C, D</code>. Also the code above retrieves each question number and total marks using <code>$dbQuestionNo</code> and <code>$TotalMarks</code>. </p> <p>If a question has multiple answers though, it displays two sets of answers and removes 1 answer from a set. This is because in the database, to recieve correct answer for a question which has multiple answers and single answers it is like this:</p> <pre><code>QuestionNo Answer TotalMarks OptionType 1 B 3 A-D 2 A 5 A-D 2 C 5 A-D 3 D 2 A-D </code></pre> <p><strong>UPDATE 2:</strong></p> <p>For question 2 as it is multiple answers, that is why it is displays two sets of data in the arrays. Beause the way it is removing a correct answer if there are multiple answers in a question is as so:</p> <pre><code>Question 2: Answers: A, B, C, D Remove Correct Answer: A Incorrect Answers: B, C, D Question 2: Answers: A, B, C, D Remove Correct Answer: C Incorrect Answers: A, B, D </code></pre> <p>The way it is removing correct answers if a question has multiple correct answers is above. It is:</p> <ul> <li>Displaying Possible Answers</li> <li>Removes 1 Correct Answer <code>A</code></li> <li>Displays Incorrect Answers <code>B, C, D</code></li> </ul> <p>To Remove second correct Answer it repeats the process:</p> <ul> <li>Displaying Possible Answers</li> <li>Removes 1 Correct Answer <code>C</code></li> <li>Displays Incorrect Answers <code>A, C, D</code> (We know <code>A</code> is not correct but because it is only removing one correct answer at a time and displays all possible answers, it acts as if <code>A</code> is incorrect in second set but correct in first set)</li> </ul> <p>UPDATE 3:</p> <p>The error is caused by the fact that array you are iterating over $ques_ans with for() has a gap in keys.</p> <pre><code>var_dump($ques_ans) gives us: array(3) { ... skipped for brevity [2]=&gt; array(2) { [0]=&gt; string(1) "B" [2]=&gt; string(1) "D" } ... skipped for brevity } </code></pre> <p>There is no element with the key <a href="https://i.stack.imgur.com/GNjty.png" rel="nofollow noreferrer">1</a>. It's because the function array_intersect, which I use on line 49, preserves keys.</p> <p>To quickly fix code just to make it work without errors I added array_values() on line 51: <code>$ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans</code> against the ques no as key.</p> <p>But I still get the odd gap in <code>$keys</code>.</p> <p><strong>HERE IS THE CODE:</strong></p> <p>Below is 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(); $incorrect_ans = 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); $specialOptionTypes = array('Yes or No' =&gt; array( 'Yes', 'No' ),'True or False' =&gt; array( 'True', 'False' )); while ($stmt-&gt;fetch()) { // Do this for each row: if ( array_key_exists( $dbOptionType, $specialOptionTypes ) ) { $options = $specialOptionTypes[$dbOptionType]; } else if ( preg_match( '/^([A-Z])-([A-Z])$/', $dbOptionType, $match ) ) { $options = range( $match[1], $match[2] ); } else { // issue warning about unrecognized option type $options = array(); } $right = str_split( $dbAnswer ); $wrong = array_diff( $options, $right ); $searchQuestionNo[] = $dbQuestionNo; $searchQuestionContent[] = $dbQuestionContent; $incorrect_ans[] = $wrong; $searchAnswerId[] = $dbAnswerId; $totalMarks[] = $dbQuestionMarks; $searchMarks[] = $dbQuestionMarks; } ?&gt; &lt;/head&gt; &lt;body&gt; &lt;?php $ques_ans = array(); //to store incorrect answers against ques no. $q_occ_count = array_count_values($searchQuestionNo); foreach ($searchQuestionNo as $key =&gt; $questionNo) { if (!array_key_exists($questionNo, $ques_ans)) { if ($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans { $ques_ans[$questionNo] = $incorrect_ans[$key]; //store the array of incorrect ans against the ques no as key } else //if a ques has more than 1 correct ans { //find the intersection of incorrect_ans arrays for this ques $q_keys = array_keys($searchQuestionNo, $questionNo); $q_incorrect_ans = $incorrect_ans[$q_keys[0]]; foreach ($q_keys as $q_key) { $q_incorrect_ans = array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]); } $ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans against the ques no as key } } } var_dump($ques_ans); ?&gt; &lt;table id='penaltytbl'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class='questionth'&gt;Question No.&lt;/th&gt; &lt;th class='questionth'&gt;Question&lt;/th&gt; &lt;th class='incorrectanswerth'&gt;Incorrect Answer&lt;/th&gt; &lt;th class='answermarksth'&gt;Marks per Answer&lt;/th&gt; &lt;th class='totalmarksth'&gt;Total Marks&lt;/th&gt; &lt;th class='noofmarksth'&gt;Marks Remaining&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php foreach ($ques_ans as $questionNo =&gt; $inc_ans) { $q_row_span = count($inc_ans); $row_count = 0; ?&gt; &lt;tr class="questiontd"&gt; &lt;td class="questionnumtd q&lt;?php echo $questionNo; ?&gt;_qnum" rowspan="&lt;?php echo $q_row_span; ?&gt;"&gt;&lt;?php echo $questionNo; ?&gt; &lt;input type="hidden" name="numQuestion" value="&lt;?php echo $questionNo; ?&gt;" /&gt; &lt;input type="hidden" name="q&lt;?php echo $questionNo; ?&gt;_ans_org" class="q&lt;?php echo $questionNo; ?&gt;_ans_org" value="&lt;?php echo $searchMarks[array_search($questionNo, $searchQuestionNo)]; ?&gt;"&gt; &lt;input type="hidden" name="q&lt;?php echo $questionNo; ?&gt;_ans" class="q&lt;?php echo $questionNo; ?&gt;_ans" value="&lt;?php echo $searchMarks[array_search($questionNo, $searchQuestionNo)]; ?&gt;"&gt; &lt;/td&gt; &lt;td class="questioncontenttd" rowspan="&lt;?php echo $q_row_span; ?&gt;"&gt;&lt;?php echo $searchQuestionContent[array_search($questionNo, $searchQuestionNo)]; ?&gt; &lt;/td&gt; &lt;td class="answertd"&gt;&lt;?php echo $inc_ans[$row_count]; ?&gt; &lt;input type="hidden" id="hiddenincorrect" name="incorrect[]" value="&lt;?php echo $inc_ans[$row_count]; ?&gt;"&gt; &lt;/td&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q&lt;?php echo $questionNo; ?&gt;_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="&lt;?php echo $questionNo; ?&gt;" onkeypress="return isNumberKey(event)" maxlength="3" /&gt; &lt;/td&gt; &lt;td class="totalmarkstd" rowspan="&lt;?php echo $q_row_span; ?&gt;"&gt;&lt;?php echo $totalMarks[array_search($questionNo, $searchQuestionNo)]; ?&gt;&lt;/td&gt; &lt;td class="noofmarkstd q&lt;?php echo $questionNo; ?&gt;_ans_text" q_group="1" rowspan="&lt;?php $q_row_span; ?&gt;"&gt;&lt;?php echo "&lt;strong&gt;" . $searchMarks[array_search($questionNo, $searchQuestionNo)] . "&lt;/strong&gt;"; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php //remaining incorrect answers in separate row (if any) follows here if ($row_count &lt; $q_row_span - 1) { for ($i = ($row_count + 1); $i &lt; $q_row_span; $i++) { ?&gt; &lt;tr&gt; &lt;td class="answertd"&gt;&lt;?php echo $inc_ans[$i]; ?&gt; &lt;input type="hidden" id="hiddenincorrect" name="incorrect[]" value="&lt;?php echo $inc_ans[$i]; ?&gt;"&gt; &lt;/td&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q&lt;?php echo $questionNo; ?&gt;_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="&lt;?php echo $questionNo; ?&gt;" onkeypress="return isNumberKey(event)" maxlength="3" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } } } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt; &lt;input type='hidden' id='num_groups' name='num_groups' value='&lt;?php echo $questionNo; ?&gt;'&gt; &lt;input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" /&gt; &lt;/p&gt; &lt;/form&gt; </code></pre> <p><strong>SCREENSHOT:</strong></p> <p><img src="https://i.stack.imgur.com/GNjty.png" alt="enter image description here"></p>
 

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