Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several errors in your script, which explains why it doesn't work. First, your <code>$response</code> array doesn't seem right. Did you <em>really</em> intend to use variables in place of keys? Try <code>print_r($response);</code> and see if it's really what you want.</p> <p>You can select the nth <code>&lt;question/&gt;</code> using the array notation (0-based) as in</p> <pre><code>$results-&gt;question[$i] </code></pre> <p>Once you get the right question, all you need to do is verify that it does indeed contain the suggested answer with XPath. Then only, you can <a href="http://docs.php.net/manual/en/language.operators.increment.php" rel="nofollow noreferrer">increment</a> the value of <code>&lt;responses/&gt;</code>. Also, you have to escape special characters such as <code>&lt;</code> or <code>"</code>, which would make your XPath query fail.</p> <p>Finally, you can use <code>asXML()</code> to actually save the file (no <code>file_put_contents()</code> needed here.) I assume that your <code>$surveyResults</code> is a typo and that you meant <code>$surveyResultsFile</code>.</p> <pre><code>$response = array( 'sid' =&gt; session_id(), 'answers' =&gt; array( $_POST['input1'] ) ); $results = simplexml_load_file($surveyResultsFile); foreach ($response['answers'] as $i =&gt; $answer) { $title = htmlspecialchars($answer); $a = $results-&gt;question[$i]-&gt;xpath('answer[title="' . $title . '"]'); if (empty($a)) { echo "No answer '$title' for question $i\n"; continue; } ++$a[0]-&gt;responses; } $results-&gt;asXML($surveyResultsFile); </code></pre>
 

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