Log in

View Full Version : Using XML



jfreak53
01-24-2009, 03:02 PM
I have searched and searched and have read tutorials but I still don't get it. I have been trying to get a php script of mine to work with XML but I can't get it to work. I got it to parse the file correctly and give me a print out, but I can't figure out how to delete parts and append new parts. Here is the code I am using to parse it and display:


$xmlFileData = file_get_contents("gallery.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
foreach($xmlData->image as $image) {
?>
<tr>
<td>Delete</td>
<td><? echo $image->filename ?></td>
<td><? echo $image->caption ?></td>
</tr>

This works just fine. What I need though is code for that delete button to delete a record at a time, and I need code so at the bottom of this page I can put a form for appending to this file. This code I cannot find no matter how long I search, it is getting nerve racking. :(

Thanks for any help you can give me.

azzozhsn
01-24-2009, 06:35 PM
Well, you need to add a key for each record:


$xmlFileData = file_get_contents("gallery.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
foreach($xmlData->image as $key=>$image) {
?>
<tr>
<td><a href="?delete=$key">Delete</a></td>
<td><? echo $image->filename ?></td>
<td><? echo $image->caption ?></td>
</tr>


after that you need to remove that record from the list:


foreach($xmlData->image as $key=>$image) {
if (intval($_GET['delete']) == $key) {
// delete the line
}
}
......

jfreak53
01-24-2009, 11:31 PM
Thanks, question though. What is the delete command to actually delete the part of the xml file? And what is the command to append a new area to the xml file. This is what my xml file looks like:


<simpleviewerGallery maxImageWidth="480" maxImageHeight="360" textColor="0xFFFFFF" frameColor="0xffffff" frameWidth="10" stagePadding="20" thumbnailColumns="3" thumbnailRows="3" navPosition="left" title="miaDios Galeria" enableRightClickOpen="false" backgroundImagePath="" thumbPath="" imagePath="" >
<image>
<filename>1.jpg</filename>
<caption>Caption 1</caption>
</image>
<image>
<filename>1.jpg</filename>
<caption>Caption 2</caption>
</image>
</simpleviewerGallery>

What I want to delete is a whole image area and add new image areas.