Search code examples
phpxmlphp4

PHP 4 read and write XML


I have found several questions and answers pertaining to my question, but I'm getting a little confused. Perhaps clarification from someone would help.

Sadly, I am working in PHP 4 - no chance of upgrading. :( (See comments below... I'm stuck working on a client server who doesn't want to upgrade from a folksy/non-PHP-updating web host.)

My aim is to make a web-based GUI that updates the contents of an XML Flash gallery file. So basically the XML file is nothing more than this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
    <pic>
        <image>images/image1.jpg</image>
    </pic>
    <pic>
        <image>images/image2.jpg</image>
    </pic>
     <pic>
        <image>images/image3.jpg</image>
    </pic>
</images>

The backend, I'm hoping, is simply going to look like text boxes with the URLs for the images and an "update" button. They'll also be able to add another image by clicking "add image".

Now, I found this question: Read and write an XML file with PHP which seemed to answer what I was looking for. My only concern is, let's say we want to edit the URL of the second image, and there are 8 images? Not to mention, how to populate the text boxes?

The only idea I had was to create a function that basically writes something like:

<pic>
    <image>$_POST['textbox1']</image>
</pic>

and it just loops through each textbox, writes that, then writes a new XML file each time there is any update?

Perhaps I'm making this more complicated than it should be.


Solution

  • Why not simply define the textboxes as the following?

    <input type="text" name="picture[]"/>
    

    Then in PHP you just access all values using

    foreach ($_POST['picture'] as $pictureUrl)
    {
        $buffer.= '<pic><image>'.$pictureUrl.'</image></pic>';
    }
    

    You definitely want to check whether the array is set or not, etc.