August 11, 201115 yr Hi everyone! I have an XML file as below: <?xml version="1.0" encoding="utf-8"?> <Piecemaker> <Contents> <Image Source="image/piecemaker1.jpg" Title="title1"></Image> <Image Source="image/piecemaker2.jpg" Title="title2"></Image> <Image Source="image/piecemaker3.jpg" Title="title3"></Image> <Image Source="image/piecemaker4.jpg" Title="title4"></Image> </Contents> </Piecemaker> OK, so... its actually a Piecemaker 2 Slider. Now, I want PHP to update the XML file on the fly every time I upload a new image. So the new XML file will be like this: <?xml version="1.0" encoding="utf-8"?> <Piecemaker> <Contents> <Image Source="image/newImage1.jpg" Title="title1"></Image> <Image Source="image/newImage2.jpg" Title="title2"></Image> <Image Source="image/newImage3.jpg" Title="title3"></Image> <Image Source="image/newImage4.jpg" Title="title4"></Image> </Contents> </Piecemaker> Any idea? I failed to use SimpleXML to do this. Thanks.
August 11, 201115 yr Author Ok forget about that! Neither will work with that approach. Sometimes the simplest solution that counts! Any idea how to add a document versioning on that XML files? <?xml version="1.0" encoding="utf-8"?> <Piecemaker> <Contents> <Image Source="image/piecemaker1.jpg<?php $now = time(); echo $now; ?>" Title="title1"></Image> <Image Source="image/piecemaker2.jpg<?php $now = time(); echo $now; ?>" Title="title2"></Image> <Image Source="image/piecemaker3.jpg<?php $now = time(); echo $now; ?>" Title="title3"></Image> <Image Source="image/piecemaker4.jpg<?php $now = time(); echo $now; ?>" Title="title4"></Image> </Contents> </Piecemaker> My idea is just to force the slider page (index.php) to get the latest image file from my XML file. Edited August 11, 201115 yr by Monie
August 11, 201115 yr if you want to get the last/latest image then you can do something like $string = <<<XML <Piecemaker> <Contents> <Image Source="image/piecemaker1.jpg" Title="title1"></Image> <Image Source="image/piecemaker2.jpg" Title="title2"></Image> <Image Source="image/piecemaker3.jpg" Title="title3"></Image> <Image Source="image/piecemaker4.jpg" Title="title4"></Image> </Contents> </Piecemaker> XML; $xml = simplexml_load_string($string); echo($xml->Contents->Image[count($xml->Contents->Image)-1]['Source']);
August 12, 201115 yr Author if you want to get the last/latest image then you can do something like $string = <<<XML <Piecemaker> <Contents> <Image Source="image/piecemaker1.jpg" Title="title1"></Image> <Image Source="image/piecemaker2.jpg" Title="title2"></Image> <Image Source="image/piecemaker3.jpg" Title="title3"></Image> <Image Source="image/piecemaker4.jpg" Title="title4"></Image> </Contents> </Piecemaker> XML; $xml = simplexml_load_string($string); echo($xml->Contents->Image[count($xml->Contents->Image)-1]['Source']); Hi Jock, Can you tell me what does the code do? See, the XML file is static. So every time I upload an image, it will be stored in the same directory with the same file name (either piecemaker1.jpg, 2, 3, or 4). In order for the page slider page to fetch the new "piecemaker1.jpg" instead of the old cache version of "piecemaker1.jpg", I had to manually refresh the page a few times, which is a bad things to ask the user to refresh their browser! The code you gave me, is it in XML format of PHP? Do you have any better solution to my issue? Thanks Jock!
August 12, 201115 yr Author Ok, a change of knowledge just happen to me! What I want to do is actually to update my xml file (Source attributes of the Image node) to a newly uploaded image. Here is the code: <?xml version="1.0" encoding="utf-8"?> <Piecemaker> <Contents> <Image id="1" Source="image/piecemaker1.jpg" Title="piecemaker1"></Image> <Image id="2" Source="image/piecemaker2.jpg" Title="piecemaker2"></Image> <Image id="3" Source="image/piecemaker3.jpg" Title="piecemaker3"></Image> <Image id="4" Source="image/piecemaker4.jpg" Title="piecemaker4"></Image> </Contents> </Piecemaker> Ok, somewhere in my PHP image upload function, right after successfully uploaded the new m=image, I want to perform the xml update to update the xml Source attributes to the newly uploaded image name and Title. //PHP image upload function... //XML Update attributes I want to be able to loop to the xml files to find the selected ID and later change the Title attributes (or Source attributes): $id = 1; $new_title = "new title"; $xml = simplexml_load_file('file.xml'); foreach($xml->Piecemaker->Contents->Image as $linkGroup){ $linkGroup['Title'] = $new_title; break; } file_put_contents('file.xml', $xml->asXml()); Honestly, I dont even know if those xml code I right up there make sense or not! But that is the condition that I want to achieve. Consider that as a pseudocode Thanks.
August 15, 201115 yr something like this maybe? http://stackoverflow.com/questions/194574/inserting-data-in-xml-file-with-php-dom
August 16, 201115 yr Author Almost like that, let me refresh everyone with what I want to achieve here. I have this XML file (sample.xml): <?xml version="1.0" encoding="utf-8"?> <Piecemaker> <Contents> <Image id="1" Source="image/piecemaker1.jpg" Title="piecemaker1"></Image> <Image id="2" Source="image/piecemaker2.jpg" Title="piecemaker2"></Image> <Image id="3" Source="image/piecemaker3.jpg" Title="piecemaker3"></Image> <Image id="4" Source="image/piecemaker4.jpg" Title="piecemaker4"></Image> </Contents> </Piecemaker> Its for a slider on my main page. I want to limit the number of slider to 4 image only. Nothing more. So, I created an HTML form (four upload form for each image) for me to insert the image and eventually update the XML file above with the newly uploaded image file name. So what I want the PHP to do is, right after the script to insert the image is successfully done, then a script will be executed to update the existing xml file with the newly uploaded image file name: upload-slider1.php Form Image:__________(Browse) Title:__________ Submit submit-image.php $new_file_name = $_POST['image']; $title = $_POST['title']; Upload image to directory here! .. . // Loop the XML file if image id == "1", or 2,3,4 set the Source to "&new_file_name" set the Title to "$title" update the XML file save document I have issue in the xml looping part and updating the existing xml attributes as well. Edited August 16, 201115 yr by Monie
August 17, 201115 yr Author Ok, after surfing for a few hours learning PXP-XML thingy, I managed to magically update my XML file with this PHP code using DOMDocument function: // --- Updating the XML File ------------------------------------------- // $xml = new DOMDocument(); $xml->load("../piecemaker.xml"); $root=$xml->documentElement; //find the tags that you want to update $tags = $root->getElementsByTagName('Image'); //find the tag with the id you want to update foreach ($tags as $tag) { if($tag->getAttribute("id") == "slide1") { //found the tag, now update the attribute $tag->setAttribute("Source", "newImageName.jpg"); $tag->setAttribute("Title", "newImageTitle"); } } //save the xml $xml->save("..piecemaker.xml"); The issue now is it removed some end tag in the xml file: // Original Structure <Contents> <Image id="1" Source="image/piecemaker1.jpg" Title="piecemaker1"></Image> </Contents> // After successfully update, the </Image> is missing <Contents> <Image id="1" Source="newImageName.jpg" Title="newImageTitle"> </Contents> I am wrong somewhere? Edited August 17, 201115 yr by Monie
August 18, 201115 yr Author Well, I guess no one knows much about XML here. So I dig my very own research on this issue and managed to solve what it seems to be a big issue but actually a small one. Thanks anyway for the effort guys. Appreciate it!
Create an account or sign in to comment