tags:

views:

102

answers:

4

1st I have to say I know nothing bout php. I was actually doing my AS3 guest-book and through parts of tutorials from Activetut, I managed to come out a flash guest-book. So the problem now I'm facing is the guest-book could only inject 1 XML data and it will always clear off the old 1, while the flash is still caching on the old XML files.

I'd found some other tutorials(which I think its quite hard since i dunno anything about php) and comparing to the php code I'm using, it seems to be extremely short. I have no idea what the code does, so currently I'm not sure whether the problems came from the php or my AS3.

 <?php
        if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
            $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
            $file = fopen("wish.xml","wb");
            fwrite($file, $xml);
            fclose($file);
        }
    ?>

and below is my correct XML format:

<WISHES>
    <WISH>
        <NAME>Test</NAME>
        <EMAIL>[email protected]</EMAIL>
        <DATENTIME>2/3/10</DATENTIME>
        <MESSAGE>Dummy Message</MESSAGE>
    </WISH>
<WISH>
        <NAME>Test</NAME>
        <EMAIL>[email protected]</EMAIL>
        <DATENTIME>2/3/10</DATENTIME>
        <MESSAGE>Dummy Message</MESSAGE>
    </WISH>
</WISHES>

So anyone kind to explain what that php code does? cause it replace my XML with:

<WISH>
        <NAME>Test</NAME>
        <EMAIL>[email protected]</EMAIL>
        <DATENTIME>2/3/10</DATENTIME>
        <MESSAGE>Dummy Message</MESSAGE>
    </WISH>
+2  A: 

The code takes the value of $GLOBALS["HTTP_RAW_POST_DATA"] and writes that to wish.xml, deleting the previous file content.

Coronatus
isit? how could i change it to just add a new 1 instead of keep deleting others?
Hwang
$xml = new SimpleXMLElement($fileContents); $xml->addChild($newElement);See http://uk3.php.net/manual/en/simplexmlelement.addChild.php
Coronatus
A: 
<?php
       // Checks if $GLOBALS["HTTP_RAW_POST_DATA"] contains any data
       if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){

           // puts the data in $GLOBALS["HTTP_RAW_POST_DATA"]
           // into the variable $xml
           $xml = $GLOBALS["HTTP_RAW_POST_DATA"];

           // opens a filehandle `$file` to the file wish.xml
           // The 'w' means that if the file exists all content is removed
           // The 'b' basicly means 'binary write' (good for portability) 
           $file = fopen("wish.xml","wb");

           // writes the data from $xml to the file
           fwrite($file, $xml);

           // close the file handle
           fclose($file);
       }
?>
Nifle
A: 

Try out "ab" instead of "wb" in fopen.

wb : open for writing. The 'b' indicates binary data.

ab : open for appending. The 'b' indicates binary data.

CM
A: 

As Michael and Nifle already stated, the code deletes the current entries in the xml file when storing a new entry. To overcome this problem and do, what you expected, amend the code this way:

<?php
    if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
        $xml = $GLOBALS["HTTP_RAW_POST_DATA"];

        // read in the old ontent of the file
        $oldXML = join('', file('wish.xml'));

        // replace the old file ending with the new entry and a new file ending
        $newXML = str_replace('</WISHES>', $xml."\n</WISHES>");

        // finally store the new data to the file
        $file = fopen("wish.xml","w");
        fwrite($file, $newXML);
        fclose($file);
    }
?>
Cassy