Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When your code get to <code>$results = $_POST['value'];</code>, <code>$question_ids</code> is populated with the ids of questions that must be included in the table.</p> <p>My suggestion is to remove elements from this array right after including the first answer to the related question. This way, after the <code>foreach($results as $id =&gt; $value)</code> loop, this array will contain only questions without explicit answers. The next step is just to include these pseudo-answer "No answer" in the DB.</p> <p>The relevant code for you (inserted lines are //* commented):</p> <pre><code>$notAnswered = $question_ids; //* Make a copy $results = $_POST['value']; foreach ($results as $id =&gt; $value) { $answer = $value; $quesid = (int)$question_ids[$id]; $pos = array_search($quesid, $notAnswered); //* Search for it if ($pos !== false) //* It's in the array array_splice($notAnswered, $pos, 1); //* Delete it from the array foreach ($value as $answer) { $insertanswer-&gt;bind_param("is", $quesid, $answer); $insertanswer-&gt;execute(); if ($insertanswer-&gt;errno) { // Handle query error here echo __LINE__.': '.$insertanswer-&gt;error; break 7; } } } //* Insert 'No Answer' for each question not answered foreach ($notAnswered as $id) { $insertanswer-&gt;bind_param('is', $id, 'No Answer'); $insertanswer-&gt;execute(); if ($insertanswer-&gt;errno) { // Handle query error here echo __LINE__.': '.$insertanswer-&gt;error; break 7; } } // Close your statements at the end $insertanswer-&gt;close(); </code></pre> <p>One important thing to note here: array_splice is <em>very</em> slow in PHP for big arrays (>100 elements). But I don't think this is the case 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