-3

I have an XML file held on a server as below. I then have an HTML file with a button that when pressed must plus one (+1) to one of my XML node values.

Im not too clued up on php so any help would be great. but i need a simple script that is stored on the server that will take in an html request and add 1 to my chosen XML node value.

<?xml version="1.0" encoding="UTF-8"?>

<object1>
    <value>10</value>
</object1>

<object2>
    <value>6</value>
</object2>
madth3
  • 7,151
  • 12
  • 47
  • 72
Dustin Silk
  • 3,810
  • 4
  • 29
  • 44

1 Answers1

0

Try something like this:

$objectX = "2"; // You get this value with $_POST or $_GET ...
$xmlFileName = 'my.xml'; // You're XML file

$xmlFile = file_get_contents($xmlFileName); // Saving the XML contents in a variable
$objects = new SimpleXMLElement($xmlFile);

$objectX = "object".$objectX; // The object name
$objects->$objectX->value++; // Incrementing the value
$objects->asXML($xmlFileName); // Saving the XML

echo $objects->$objectX->value; // echo the value

You have to add <objects></objects> to your XML file:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
    <object1>
        <value>10</value>
    </object1>
    <object2>
        <value>6</value>
    </object2>
</objects>
HamZa
  • 14,051
  • 11
  • 52
  • 72