PDA

View Full Version : PHP Using PHP to write to existing XML file



joshjaybee
December 8th, 2008, 01:54 AM
Hello,

I'm trying to develop a backend for a gallery that uses Flash and XML. I've come to the stage of getting very angry with myself that I cannot get this to work, I've tried so many different ways and ideas and tutorials and snippets to try and get this to work, but nothing.

I did discover this http://www.kirupa.com/net/writingXML_pg4.htm (http://www.kirupa.com/net/writingXML_pg4.htm) and thought what a great resource exactly what I want, except it's for .NET. So I went about transferring it to PHP. But alas Epic Failure!

Just need some help on what’s wrong here I've posted both the XML document and the php code. Basically I'm trying to add images to an album. Once I get this working (with your help) I will start on the form to submit the pictures and also work on another php class to create a new album, instead of just having the 2 that are in the XML file. You will understand this when you see the XML file.

I'll keep this a working project on here for others who are in the same boat. I feel using Flash and XML for galleries is great however manually entering data is not my idea or a complete system.

Thanks

Josh


<?php
$xmldoc = loadXMLDoc('gallery.xml');

function AddImage($title, $date, $thumbnail, $image, $description)
{
//
// Initialize albumsElement if you are adding your first album.
//
$albumsElement = $xmlDoc.GetElementById("album");

//
// Adding and populating the elements
//
$imageElement = xmlDoc.CreateElement("image");
$descriptionElement.InnerText = $description;
$imageElement.AppendChild($descriptionElement);

$imageAttribute = xmlDoc.CreateAttribute("title");
$imageElement.SetAttributeNode($imageAttribute);
$imageAttribute.Value = $title;
$imageAttribute = xmlDoc.CreateAttribute("date");
$imageElement.SetAttributeNode($imageAttribute);
$imageAttribute.Value = $date;
$imageAttribute = xmlDoc.CreateAttribute("thumbnail");
$imageElement.SetAttributeNode($imageAttribute);
$imageAttribute.Value = $thumbnail;
$imageAttribute = xmlDoc.CreateAttribute("image");
$imageElement.SetAttributeNode($imageAttribute);
$imageAttribute.Value = $image;

$albumsElement.AppendChild($imageElement);
}

function WriteToDisk($path)
{
// Add the albumsElement to our root element and write to disk
$xmlDoc->AppendChild($albumsElement);
$xmlDoc->Save($path);
}


AddImage("Image One", "21/04/07", "c1.jpg", "c1.jpg", "A test image for this purpose.");

WriteToDisk(@"gallery.xml");
?>


<?xml version="1.0" encoding="iso-8859-1"?>
<gallery>

<!-- configure the gallery in this xml-node -->
<config>
<!-- set the title of the album -->
<title>YBC PHOTOS!</title>
<!-- set the location of the thumbnails relative to the gallery.swf -->
<thumbnail_dir>images/thumbs/</thumbnail_dir>

<!-- set the actlocation of the actual high-res images relative to the gallery.swf -->
<image_dir>images/big/</image_dir>

<!-- the time interval for each image to display, this value is in seconds
default value : 5
-->
<slideshow_interval>5</slideshow_interval>
<!-- pause slideshow at start
default value : false
-->
<pause_slideshow>false</pause_slideshow>
<!-- set true if you want the application to scale the images while using RSS feeds
default value : true
-->
<rss_scale_images>true</rss_scale_images>
<!-- set the path of the mp3 file that should loop as a background music while playing the slideshow.
default value : none
-->
<background_music>Im_Still_Here.mp3</background_music>
<!-- percentage of the volume to set while playing the music
default value : 50
-->
<background_music_volume>50</background_music_volume>
<!-- add links to images, and allow the views to click on images from flickr, picasa,and
view the page where the actual image is found.
If you dont want views to visit the picasa, or flickr or other sites keep this value false.
default value : false
-->
<link_images>false</link_images>
<!--
Photographers will like to disable printscreen for their pictures
to increase the security of their photographs.
Set this value to true if you wish to disable print screen in the application.
While the application is running the print screen option will be completely disabled in
all other applications that the visitor is using.
default value : false
-->
<disable_printscreen>false</disable_printscreen>

</config>


<!-- this node contains all the albums -->
<albums>
<album title="Test Gallery" description="This is a testing gallery.">
<image title="Test Image One" date="21/04/07" thumbnail="c1.jpg" image="c1.jpg">A perfect shot of a white tiger walking in the woods</image>
<image title="Test Image Two" date="29/05/07" thumbnail="c2.jpg" image="c2.jpg"> This beast scared us by giving us a really sudden shot.</image>
</album>

<album title="Test Gallery2" description="This is a testing gallery.">
<image title="Test Image Three" date="21/04/07" thumbnail="c3.jpg" image="c3.jpg">A perfect shot of a white tiger walking in the woods</image>
<image title="Test Image Four" date="29/05/07" thumbnail="c4.jpg" image="c4.jpg"> This beast scared us by giving us a really sudden shot.</image>
</album>
</albums>

<!-- language support added in v 1.0b -->

<language>
<string id="please wait" value="Please wait" />
<string id="loading" value="Loading" />
<string id="previous page" value="Previous Page" />
<string id="page % of %" value="Page % of %" />
<string id="next page" value="Next Page" />
</language>
</gallery>

Templarian
December 8th, 2008, 11:19 AM
http://us3.php.net/simplexml Is usually easier for most to understand. But good work.

joshjaybee
December 8th, 2008, 06:02 PM
http://us3.php.net/simplexml Is usually easier for most to understand. But good work.

Thanks but it’s not really good work until it's working :P, I did have a look at simpleXML however the problem lies with loading xml documents and adding elements, It's not as stable as using DOM, It also tends to have memory leaks. However if someone can prove me wrong with working example that work's with mine I will change my tune.

I still haven’t worked out my problem yet and it really is driving me bonkers! I can only find out how this works properly in flash, however I really don't want to have to use flash for just a form.

Someone has bound to have done this!

Josh

joshjaybee
December 10th, 2008, 06:07 PM
Sorry for the double post but I really need this!

Where is kirupa! Or someone who knows this stuff inside out..

:(

joshjaybee
December 14th, 2008, 11:58 PM
Ok, so I've cleaned up the code abit more, worked out some issues, but i still can't get it to work!

Am I trying to do something that cannot be done? Because if this is the case I'll be rather peeved!


<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load('gallery.xml');

function AddImage($title,$date,$thumbnail,$image,$descripti on)
{
//
// Initialise albumsElement
//
$albumsElement = $xmlDoc->GetElementsByTagName("album");

//
// Create image element
//
$imageElement = $xmlDoc->createElement("image");
$xmlDoc->appendChild($imageElement);
//
// Add text node to image element
//
$imageDesc = $xmlDoc->createTextNode($description);
$imageElement->appendChild($imageDesc);
//
// Create title attribute to image element
//
$imageTitle = $xmlDoc->createAttribute("title");
$imageElement->appendChild($imageTitle);
$imageTitleValue = $dom->createTextNode($title);
$imageTitle->appendChild($imageTitleValue);
//
// Create date attribute to image element
//
$imageDate = $xmlDoc->createAttribute("date");
$imageElement->appendChild($imageDate);
$imageDateValue = $dom->createTextNode($date);
$imageDate->appendChild($imageDateValue);
//
// Create thumbnail attribute to image element
//
$imageThumbnail = $xmlDoc->createAttribute("thumbnail");
$imageElement->appendChild($imageThumbnail);
$imageThumbnailValue = $dom->createTextNode($thumbnail);
$imageThumbnail->appendChild($imageThumbnailValue);
//
// Create image attribute to image element
//
$imageImage = $xmlDoc->createAttribute("image");
$imageElement->appendChild($imageImage);
$imageImageValue = $dom->createTextNode($image);
$imageImage->appendChild($imageImageValue);
}

function WriteToDisk($nameXML)
{
// Add the albumsElement to our root element and write to disk
$xmlDoc->AppendChild($albumsElement);
$xmlDoc->Save($nameXML);
}


AddImage("Image One", "21/04/07", "c1.jpg", "c1.jpg", "A test image for this purpose.");

WriteToDisk("gallery.xml");
?>

joshjaybee
December 17th, 2008, 08:20 PM
Ok everyone I managed to work it out using SimpleXML (sorry about dissing it before Templarian).

Apprently all the "major" problems with it so far been ironed out rather well. Memory leaks are no more and better still I managed to write in 10 lines of code (without error checking yet) what would normally take 40 or so.

I have been converted and not only that it works really well. No issues with the tests I've done. I'm now just working on a simlar code for creating another album to the gallery.


<?php
function AddImage($ftitle,$fdate,$fthumbnail,$fimage,$fdesc ription,$fgallery,$nameXML){

// Load XML file
$xml = simplexml_load_file($nameXML);

// Find all instances of <album>
foreach($xml->albums[0]->album as $seg){

// Find album title you want to add to
if($seg['title'] == $fgallery) {

// Create image element
$image = $seg->addChild('image', $fdescription);

// Add title, date, thumnail, and image attribute to image element
$image->addAttribute('title', $ftitle);
$image->addAttribute('date', $fdate);
$image->addAttribute('thumbnail', $fthumbnail);
$image->addAttribute('image', $fimage);

// Save XML file
$xml->asXML($nameXML);
}
}
}

AddImage("Image One", "21/04/07", "c1.jpg", "c1.jpg", "A test image for this purpose.", "Test Gallery", "gallery.xml");

?>

Thanks guys, I'll keep you posted!

Nickstr
December 18th, 2008, 02:34 AM
Ive been trying to figure out how to use that, but i'm a complete novice when it comes to PHP :(

What im looking for is a simple form that allows me to add XML to an existing XML file, nothing fancy, no delete function or anything, just a plain old insert function.
Any help would be greatly appreciated

My xml would look like the following:



<FileDetails>
<box>
<name>Nickstr</name>
<date>12-18-08</date>
<body>Gee, i wonder if...</body>
</box>
</FileDetails>

joshjaybee
December 19th, 2008, 05:27 AM
Well what I have above in the last post is a function I created to make it easier for me to use it in a form, instead of writing heaps of code on the one form page.

Then you create your simple form, and then using $_SERVER['PHP_SELF'] in your form action, post the values to the PHP script.


<?php

require 'functions.php';

// This is your form fields
$bName = $_POST['boxName'];
$bDate = $_POST['boxDate'];
$bBody = $_POST['boxBody'];

// This is instead of addImage() as in my example
addBox($bName,$bDate,$bBody);


?>

This would be at the top of your form page, where functions.php is where your addBox function is. The names of the posts are obviously inline with the names of the form text fields.

Obviously this does not take into account error checking or form submit checking.

Hope this helps.

sadesades
November 17th, 2009, 10:30 PM
hi joshjaybee can you place a zip file of the example thanks :)

myfriendtodd
December 10th, 2009, 02:11 PM
sadesades any luck with this? does anyone have a working example of the edit, delete, insert that is still active? i get bits and pieces that seam to be very different, but i still have not seen a solid working example.

please help