Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 577629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:10:26+00:00 2026-05-13T14:10:26+00:00

I have an XML structure that looks like this: <?xml version=1.0?> <survey> <responses>0</responses> <question>

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:10:26+00:00Added an answer on May 13, 2026 at 2:10 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I'm working with a XML structure that looks like this: <ROOT> <ELEM_A> <A_DATE>20100825</A_DATE> <A_TIME>141500</A_TIME>
I have a XML file template like this that comes from some IC chips,
I have a xml that looks like this, <Parent> Running text with marked up
I have a piece of XML that is structured similar to this: <root> <score
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following XML structure: <?xml version=1.0 encoding=utf-8 standalone=yes?> <Root xmlns:xsi=My Program> <NotRoot
I want to write a stored procedure that queries XML files after I have
In C#, assume that I have an XElement (say myXElement ) containing some XML
I have to following XML document structure: <option_set id=1> <option>Yes</option> <option>No</option> <option>Maybe</option> </option_set> <question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.