LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   php fwrite duplicates output (http://www.linuxquestions.org/questions/programming-9/php-fwrite-duplicates-output-926051/)

bluegospel 01-27-2012 05:11 PM

php fwrite duplicates output
 
Hi. I've got 2 files, an xml file and a a php script, included in another file. When the form is submitted, I want the script to insert the new line at the top of the xml, but instead it inserts the line three times. The culprit code is marked by comments.

Here's the XML:

Code:

<?xml version='1.0' ?>

<guestbook>

<guest><name>Barth Jones (Blue Bounty)</name><date>January 25, 2012, 12:30 PM</date></guest>

<guest><name>Jackie Jones</name><date>January 27, 2012, 12:30 PM</date></guest>

</guestbook>

Here's the PHP:

Code:

<?php

if($_GET['signedthissession']==true)
        {
        $guestbookfromfile="xml/guestbook.xml";

        $domguestbook = new DOMDocument();
        $domguestbook->load($guestbookfromfile);
        $guestsroot=$domguestbook->documentElement;

        $guestlist=$guestsroot->getElementsByTagName('guest');
       
        foreach($guestlist as $guest)
                {
                $guestname=$guest->firstChild->nodeValue;
                $guestdate=$guest->firstChild->nextSibling->nodeValue;
                $guestnode="<guest><name>$guestname</name><date>$guestdate</date></guest>";
                $guestbook[]=$guestnode;
                }

        $thisguest=$domguestbook->createElement('guest');
        $thisguestname=$domguestbook->createElement('name', $_GET['guestname']);
        $thisguestdate=$domguestbook->createElement('date', date("D M j, 2011, g:i A"));
        $thisguest->appendChild($thisguestname);
        $thisguest->appendChild($thisguestdate);

        $thisguestname=$thisguest->firstChild->nodeValue;
        $thisguestdate=$thisguest->firstChild->nextSibling->nodeValue;
        $thisguest="<guest><name>$thisguestname</name><date>$thisguestdate</date></guest>";
        array_unshift($guestbook, $thisguest);

        $newguestbook="<?xml version='1.0' ?>\n\n<guestbook>\n\n";

        foreach($guestbook as $aguest)
                {
                $newguestbook.=$aguest . "\n\n";
                }

        $newguestbook.="</guestbook>\n";


//trouble begins here:

        //var_dump($newguestbook);        //this output is perfect

        //but this section doesn't work as it appears it should; instead of corresponding to my var_dump it duplicates the new record three times first

        $guestbookhandle=fopen($guestbookfromfile, 'w');
        fwrite($guestbookhandle, $newguestbook);
        fclose($guestbookhandle);

//end of trouble
       
        echo('<h3 style="color:#800000">Please <a href="innerindex.php">Click Here</a> to Enter These Gates!</a></h3>');

        }else
                {
                echo('<h3 style="color:black">If you are a first-time visitor please sign our guestbook.</h3>' . "\n\n" .


                '<form action="" method="get">' . "\n\n" .
               
                '<input type="text" name="guestname" />' . "\n\n" .

                '<input type="hidden" name="signedthissession" value="true" />' . "\n\n" .

                '<input type="submit" value="Sign" style="cursor:pointer" />' . "\n\n" .


                '</form>' . "\n\n" .


                '<h3 style="color:#800000">Otherwise, please <a href="innerindex.php">Click Here</a> to Enter These Gates!</a></h3>');
                }        //end if/else

?>

Much appreciated.

Cedrik 01-27-2012 05:29 PM

Did you try to write $newguestbook in another file, just for testing ?

bluegospel 01-27-2012 08:53 PM

Quote:

Originally Posted by Cedrik (Post 4586261)
Did you try to write $newguestbook in another file, just for testing ?

I just tried as follows:

Code:

//trouble begins here:

        //var_dump($newguestbook);        //this output is perfect

        //but this section doesn't work as it appears it should; instead of corresponding to my var_dump it duplicates the new record three times first

        //$guestbookhandle=fopen($guestbookfromfile, 'w');
        //fwrite($guestbookhandle, $newguestbook);
        //fclose($guestbookhandle);
        $otherhandle=fopen("xml/otherfile.xml", "x");
        fwrite($otherhandle, $newguestbook);
        fclose($otherhandle);

//end of trouble

. . . which works as it should. But I still can't figure out why the other fails.

Cedrik 01-28-2012 05:30 AM

I don't know, I never used DOMDocument class. After a quick view in php doc, it seems it has a save method which is meant to save the xml file after making change, no ?

Maybe you have to destroy the $domguestbook object just before open the xml/guestbook.xml for writting...

PHP Code:

$domguestbook null


bluegospel 01-28-2012 09:20 AM

Quote:

Originally Posted by Cedrik (Post 4586587)
I don't know, I never used DOMDocument class. After a quick view in php doc, it seems it has a save method which is meant to save the xml file after making change, no ?

Maybe you have to destroy the $domguestbook object just before open the xml/guestbook.xml for writting...

PHP Code:

$domguestbook null


My understanding is that saveXML just takes a dom xml tree and returns it as a string so you can write it wherever you please. But your suggestion leads to another possible solution, save(), which I've never discovered.

I tried destroying $domguestbook first, but that didn't work either. Later today I'm going to try save().

bluegospel 01-28-2012 10:03 PM

Now it will probably not be until late Sunday or Monday morning that I'll try save().


All times are GMT -5. The time now is 10:15 AM.