EmailWhat Is Vitaluna?Curricula VitaeSoapBoxTech BytesAmusing BitsMiscellanyImageryNaomi PostsGareth's PostsFrances' PostsMain PageLincoln's Posts
December 2005 Archives
December 14, 2005
Simple text entry with PHP

I'm on a new path to get better at PHP/MySQL, now that we've got a new programmer on staff, and I can abuse him as a resource. A cool snipped for poor-man's content management is what I came up with today. If you have a text file that a user wants to edit, but also wants to strip the pesky slashes from, and interpret carriage returns when writing the file, here's what I came up with. It also prints the file's modification date as an "Updated last" line:

//Declare the session variable value
//declare the report variable
$report = str_replace("\r\n", "<br>", $_POST['report']);

//declare and open the file for writing
$myfile = "myfile.txt";
$fh = fopen($myfile, 'w') or die("can't open file");

//write the form data to the report
fwrite($fh, (stripslashes($report)));

Then we have the printed text, as well as the form, rolled into one file:

<? echo date( "F d\, Y", filemtime('myfile.txt') )." - Last updated"; ?>

<? include('myfile.txt'); ?>

<p>Write the daily report:<br>

<form action="<? echo $PHP_SELF ?>" method="post" name="report">
<textarea name="report" rows="9" cols="94"></textarea><br>
<input type="submit" name="submitButtonName">

PHP kicks bum.

[From the 'Tech Bytes' Section]
Posted by Lincoln at 11:58 AM