Deleting a previous variable/ or moving it

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
jeeep
Forum Commoner
Posts: 57
Joined: Fri Apr 28, 2006 1:43 am

Deleting a previous variable/ or moving it

Post by jeeep »

I have a calendar php script that I can update from a flash file. Only problem is everytime I update it cuts off because of this coming too soon

Code: Select all

</events>
Right now the only way I know how to add this is to the end of a fwrite:

Code: Select all

$writeInTxtFile = @fwrite($myTextFileHandler,"\<event title=\"$receivedFromFlashData\" description=\"once upon a time\" startDate=\"2006-10-01 15:30:00\" endDate=\"2006-10-01 17:30:00\" allDay=\"0\" eventType=\"once\"/></events>");
I have the <events> at the top of the file, where it is never bothered. But if I put </events> at the bottom of the file, it is soon over written by the next <event>, thus this is why I have it in the "fwrite".

So my question/s: Is there a way to secure the placement of </events> at the bottom of the text file? If not is there a way to delete the previous </events> and then write another to cap the file off? Thanks for any help
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

I am not sure if this is what you are looking for, but I use it alot in loops.

Code: Select all

<?php
// destroy a single variable
unset($foo);

// destroy a single element of an array
unset($bar['quux']);

// destroy more than one variable
unset($foo1, $foo2, $foo3);


or re define the variable  $var="";
then it will get updated at next loop to whatever


?>
Hope this helps
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can read the old contents, replace </events> by the new entry+</events> and then write the contents back to the file.

Code: Select all

<?php
$filename = 'whatever.xml';
$contents = file_get_contents($filename);
if ( empty($contents) ) {
	$contents = '<events></events>';
}

// $receivedFromFlashData = rand(100,999);
$newEntry = '<event title="' . $receivedFromFlashData . '" description="once upon a time" startDate="2006-10-01 15:30:00" endDate="2006-10-01 17:30:00" allDay="0" eventType="once"/></events>';
$contents = str_replace('</events>', $newEntry, $contents);
file_put_contents($filename, $contents);


Or you can set the file pointer back

Code: Select all

<?php
$closeEvents = '</events>';

// $receivedFromFlashData = rand(100,999);
$newEntry = '<event title="' . $receivedFromFlashData . '" description="once upon a time" startDate="2006-10-01 15:30:00" endDate="2006-10-01 17:30:00" allDay="0" eventType="once"/>'
		. $closeEvents;

$filename = 'whatever.xml';
$myTextFileHandler = fopen($filename, 'r+');
fseek($myTextFileHandler, -strlen($closeEvents), SEEK_END);
fwrite($myTextFileHandler, $newEntry);

fclose($myTextFileHandler);
jeeep
Forum Commoner
Posts: 57
Joined: Fri Apr 28, 2006 1:43 am

Post by jeeep »

Thank you!
Post Reply