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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-27-2012, 04:11 PM
|
#1
|
|
Member
Registered: Jan 2010
Distribution: centOS
Posts: 381
Rep:
|
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.
|
|
|
|
01-27-2012, 04:29 PM
|
#2
|
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
Did you try to write $newguestbook in another file, just for testing ?
|
|
|
|
01-27-2012, 07:53 PM
|
#3
|
|
Member
Registered: Jan 2010
Distribution: centOS
Posts: 381
Original Poster
Rep:
|
Quote:
Originally Posted by Cedrik
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.
|
|
|
|
01-28-2012, 04:30 AM
|
#4
|
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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;
|
|
|
|
01-28-2012, 08:20 AM
|
#5
|
|
Member
Registered: Jan 2010
Distribution: centOS
Posts: 381
Original Poster
Rep:
|
Quote:
Originally Posted by Cedrik
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().
|
|
|
|
01-28-2012, 09:03 PM
|
#6
|
|
Member
Registered: Jan 2010
Distribution: centOS
Posts: 381
Original Poster
Rep:
|
Now it will probably not be until late Sunday or Monday morning that I'll try save().
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|