Page 1 of 1
writing info to a file, midway
Posted: Thu Jun 15, 2006 7:45 am
by mlecho
hi all...i am very new to php, but i find it very cool. Flash is more my gig, but i have discovered the value of these two communicating. I am making a pin up note board...literaly has notes that you can pin/de-pin. All said, the notes get there info from an XML that loads thanks to PHP. My dilema is making new notes...i understand how to write a file with php (fopen, fwrite, fclose) and i know i can add to that file, but how can i add text after the first xml tag. In this example how can i add <point> this is entry four</point> between the last and 2nd to last nodes?
Code: Select all
<entries>
<point>This is entry one</point>
<point> this is entry two</point>
<point>This is entry three</point>
<point> this is entry five</point>
</entries>
i have tried making an array with the nodes, and using <point> as a delimeter...but i am getting confused...is that the right track? Do the tags mess up PHPs ability to delimit? hmmmmm
Posted: Thu Jun 15, 2006 7:51 am
by bokehman
You need to open the file and grab the contents to an array:
Insert the data you want to add into the array and then overwrite the source file.
Posted: Thu Jun 15, 2006 8:12 am
by mlecho
thanks...i willl get back to let you know how it goes
Posted: Wed Jun 21, 2006 8:32 am
by mlecho
bokehman- just wanted to say thanks , and to follow up...so your help was right on. The file command got the arrays i needed to write to file. Final code ended up:
Code: Select all
<?php
$fileName="notepad.xml";
$newName=$_GET['newName'];
$newDate=$_GET['newDate'];
$newNote=$_GET['newNote'];
$contents=file($fileName);
$newEntry="<entry><name>$newName</name><date>$newDate</date><note>$newNote</note></entry>";
$newContents=array_splice($contents,1,0,$newEntry);
if(!($newXML=fopen($fileName,"w"))) die ("there is an error");
foreach($contents as $value){
fwrite($newXML,$value);
}
fclose($newXML);
?>
Posted: Wed Jun 21, 2006 10:33 am
by bokehman
mlecho wrote:Code: Select all
if(!($newXML=fopen($fileName,"w"))) die ("there is an error");
I'd prefer to write that like this:
Code: Select all
$newXML = @fopen($fileName,"w") or die('there was an error');
So much more readable.
Posted: Thu Jun 22, 2006 2:16 am
by mlecho
bokehman- always learning here...so the @ is the same as if(!..)? As i stated from the get-go, i am new to PHP. If you can explain @fopen, would love it...thanks
Posted: Thu Jun 22, 2006 2:19 am
by feyd
Posted: Thu Jun 22, 2006 2:34 am
by bokehman
mlecho wrote:bokehman- always learning here...so the @ is the same as if(!..)? As i stated from the get-go, i am new to PHP. If you can explain @fopen, would love it...thanks
The @ just suppresses the error from the fopen. There's no point having two error messages; you already have one in the
die().
This works because there is an "
or". If the first statement returns false the second will be run. It's as simple as that. This construction seems really common when used in conjunction with mysql queries but works fine everywhere.