I have an XML structure that looks like this:
<?xml version="1.0"?>
<survey>
<responses>0</responses>
<question>
<title>Some survey question</title>
<answer>
<title>Answer 1</title>
<responses>0</responses>
</answer>
<answer>
<title>Answer 2</title>
<responses>0</responses>
</answer>
...
</question>
...
</survey>
I want to increment the <responses>
values for answers that match the values in a $response
array. Here's how the $response
array is structured:
$response = array(
'sid' => session_id(),
'answers' => array(
$_POST['input1'],
$_POST['input2'],
...
)
);
I have a SimpleXMLElement called $results
for my survey xml file. Here's how I'm going about it:
$results = simplexml_load_file($surveyResultsFile);
$i = 1;
foreach($response->answers as $answer) {
$r = $results->xpath("question[$i]/answer[title='$answer']/responses");
$r = $r[0];
$r = intval($r) + 1;
$i++;
}
file_put_contents($surveyResultsFile, $results->asXML());
My results aren't being saved after incrementing the value of $r
. Any ideas on what I'm doing wrong? Thanks!