Can anyone see what’s wrong with my code? The error I’m getting is as follows:
Fatal error: Call to a member function saveXML() on a non-object in /path/to/php/file.php on line 35
Basically I’m just trying to update an XML nodeValue which changes the UI depending on the value set in the “AVAILABILITY” tag.
==================== UPDATE ====================
The XML file is now not being overwritten with the new value for the (‘STATUS’) node inside (‘AVAILABILITY’) tag, is there something I’m doing wrong?!
================================================
XML Structure
<?xml version="1.0" encoding="UTF-8"?>
<WORK>
<AVAILABILITY>
<STATUS>0</STATUS> // Not being changed?!
</AVAILABILITY>
<SLIDE>
<ID>YY001</ID>
<TITLE>YourEdentity</TITLE>
<LINK>http://youredentity.campearce.co.uk</LINK>
<THUMB>your_ed.png</THUMB>
<CAPTION>This is YourEdentity</CAPTION>
</SLIDE>
</WORK>
PHP Script:
<?php
$url = "../content/slides.xml";
$xml = simplexml_load_file($url);
if(!file_exists($url))
{
echo "Unable to locate file";
}
else
{
$root = $xml->WORK;
$slides = $root->SLIDE;
if(isset($_POST['action']) && !empty($_POST['action']))
{
$action = $_POST['action'];
switch($action)
{
case 'deleteslide' : delete_slide(); break;
case 'createslide' : create_slide(); break;
case 'changeavail' : change_avail($root, $_POST['status'], $xml, $url); break;
}
}
}
function change_avail($root_node, $status, $xml, $location){
$availability_node = $root_node->AVAILABILITY;
$status_node[0] = $availability_node->STATUS;
parse_str($status, $statustr);
$status_node[0] = $statustr;
file_put_contents($location, $xml->asXML());
echo $xml->asXML();
}
function save_toxml($xml, $location)
{
$fp = fopen($location, 'w');
fwrite($fp, $xml->asXML());
fclose($fp);
echo $xml->asXML();
}
?>
In function change_avail you didnt have variable $xml. Transmit this var: