<root>
<gallery name="First"/>
<gallery name="Second"/>
<gallery name="Third"/>
</root>
I think I have solved part of a previous problem and now have the foreach loop correct? Still I can't get it to save the xml.
$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);
foreach($objXML->xpath('/root/gallery/@name') as $key => $old){
$new = $_POST['name'][$key];
echo "$key : $old : $new<br />
";
$old = $new; // this does nothing??
}
$objXML->asXML(XML_FILE_NAME);
echo returns:
0 : First : First New
1 : Second : Second New
2 : Third : Third New
Why does this not save the new posted values for 'name' back to my XML doc, what am I doing wrong?
You're in a loop.
This mean each time the loop comes back to another point $key
and $old
get their respective new values.
I guess that's what you're trying to achieve (but I'm not you're sure I got your goal here)
foreach($objXML->xpath('/root/gallery/@name') as $key => $value){
$new = $_POST['name'][$key];
echo "$key : $old : $new<br />
";
$old = $value;
}
From your comment :
$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);
$galleryLore = $objXML->gallery; // finding gallery
$i = 0;
foreach($galleryLore as $gallery){
unset($gallery['name']); // We delete the old name
$new = $_POST['name'][$i]; // We find the new name value
$gallery->addAttribute('name', $new); // We add the new attribute we deleted before
$i++;
}