writing info to a file, midway

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

writing info to a file, midway

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

You need to open the file and grab the contents to an array:

Code: Select all

$file_array = file($filename);
Insert the data you want to add into the array and then overwrite the source file.
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post by mlecho »

thanks...i willl get back to let you know how it goes
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post 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);

?>
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post 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
Last edited by mlecho on Thu Jun 22, 2006 2:24 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have a read here.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Post Reply