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
session_start();
//declare the report variable
if(isset($_POST['report']))
{
$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)));
fclose($fh);
}
?>

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

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

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

<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">
</form>
</body>
|</html>

PHP kicks bum.

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