views:

150

answers:

2

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!

A: 

One problem might be this:

$r = $results->xpath('/question[$i]/answer[title=$answer]/responses');

Since you're using single quotes (the ' -character, is the actual English name "single quote"?), the variables in the XPath experssion aren't being evaluated, instead it's processed literally. Replace the single quotes with normal quotes (") and the variables will be processed normally.

See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

TuomasR
You also need to mark the string (literal) for xpath with quotes `title='$answer``. If $answer can possibly contain quotes you have to replace them by their entity representation, i.e. ' (or " depending on what quotes you chose to mark the string literal)
VolkerK
I made the changes, as well as removed the first forward slash which was causing problems. I can successfully increment the value of `$r` now, but changes are not being saved to the XML file.
Jared
+2  A: 

There are several errors in your script, which explains why it doesn't work. First, your $response array doesn't seem right. Did you really intend to use variables in place of keys? Try print_r($response); and see if it's really what you want.

You can select the nth <question/> using the array notation (0-based) as in

$results->question[$i]

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 increment the value of <responses/>. Also, you have to escape special characters such as < or ", which would make your XPath query fail.

Finally, you can use asXML() to actually save the file (no file_put_contents() needed here.) I assume that your $surveyResults is a typo and that you meant $surveyResultsFile.

$response = array(
    'sid'     => session_id(),
    'answers' => array(
        $_POST['input1']
    )
);

$results = simplexml_load_file($surveyResultsFile);

foreach ($response['answers'] as $i => $answer) {
    $title = htmlspecialchars($answer);
    $a = $results->question[$i]->xpath('answer[title="' . $title . '"]');

    if (empty($a))
    {
        echo "No answer '$title' for question $i\n";
        continue;
    }
    ++$a[0]->responses;
}

$results->asXML($surveyResultsFile);
Josh Davis
Thanks! The `$response` array's keys being variables was a typo, as was the `$surveyResults` instead of `$surveyResultsFile` at the end. I definitely need to study XPath and SimpleXML more, as this is my first time using them.
Jared