LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-27-2012, 04:11 PM   #1
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Rep: Reputation: 53
Question 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.
 
Old 01-27-2012, 04:29 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Did you try to write $newguestbook in another file, just for testing ?
 
Old 01-27-2012, 07:53 PM   #3
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by Cedrik View Post
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.
 
Old 01-28-2012, 04:30 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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
 
Old 01-28-2012, 08:20 AM   #5
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by Cedrik View Post
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().
 
Old 01-28-2012, 09:03 PM   #6
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Now it will probably not be until late Sunday or Monday morning that I'll try save().
 
  


Reply

Tags
duplicates, fwrite, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chemistry problem: Identify duplicates and non-duplicates within TWO sdf files robertselwyne Programming 5 12-09-2011 06:20 AM
Bash output with timestamp removing duplicates sociopathichaze Programming 3 11-21-2010 05:10 AM
trying to append data to a file with php/fwrite steve51184 Linux - Server 2 08-16-2010 08:35 PM
fwrite in another fwrite? ati Programming 2 05-11-2006 02:25 AM
how could I delete duplicates entries in xml using php catzilla Programming 2 10-30-2005 07:08 PM


All times are GMT -5. The time now is 02:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration