midge
06-17-2005, 04:27 AM
Hi! Im currently trying to impliment a small guestbook style comments program for a website, my client hasnt got access to a database program so im just using a text file. I have written PHP to get information into the text file however Im struggling to get it out in a meaningful way.
I want to sort each 'entry' by its date stamp, though Im not 100% sure how to do this as Im a PHP novice but I have coded in other languages before.
My code currently looks like this :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
// set file to read
$file = 'guestbook.txt';
// read file into string
$data = file_get_contents($file) or die('Could not read file!');
// print contents
IF $d>=Date()-'31' THEN
echo $data;
?>
</body>
</html>
Any help with this would be great thanks!
NogDog
06-17-2005, 09:14 AM
Can you give a sample of what each line in the data file looks like? (That way we can suggest how to parse it.)
akadis
06-17-2005, 10:42 AM
Once you can get each post into an array, and can seperate out the date, you can use http://maniacalrage.net/archives/2004/02/relativedatesusing/ to sort it. It's a nice function that I've found.
Since it took me a little while to figure out how to use it, and I might as well save you the effort, here's a little php code (I'm sure you can adapt it to your needs :D)
<?php
//
// Read from the file
// I decided to comment the stuff, just in case you don't get what's going on
// Get the file contents
$comments = file_get_contents ('myfile.txt');//edit: use
// if the file isn't empty:
if (!empty($comments)){
// We don't actually need the <entry></entry> but it makes for well formed XML when you write it (I'm a stickler like that)
$comments = str_replace ("<entry>", "", $comments);
$comments = str_replace ("</entry>", "", $comments);
$comments = preg_replace ("/\<title\>(.*)\<\/title\>/e", "'<dt>\\1</dt>'", $comments);
$comments = preg_replace ("/\<datetime\>(\d+)\<\/datetime\>/e", "'<dt>' . doRelativeDate('\\1') . '</dt>'", $comments);
$comments = preg_replace ("/\<msg\>(.*)\<\/msg\>/e", "'<dd>\\1</dd>'", $comments);
$comments_array = explode("<split />", $comments);
sort($comments_array);
$comments = implode("\n\t", $comments_array);
echo $comments = '<dl>' . $comments . '</dl>';
} else {
echo 'No Posts';
}
?>
<?php
//
// Write to the file
// The date
$datetime = date ('YmdHis');
// I'm assuming:
// $msg is the message (comment) itself
// $title is the title of the comment
// $file is the file where you'll keep the posts
// etc...
$comment = "\n\t<split /><entry>\n";
$comment .= "\t\t<datetime>" . $datetime . "</datetime>\n";
$comment .= "\t\t<title>" . $title . "</title>\n";
$comment .= "\t\t<msg>" . $msg . "</msg>\n";
$comment .= "\t</entry>";
if (file_exists($file)) {
if (is_writable($file)) {
if (!$handle = fopen($file, 'a')) {
die ("Cannot open file ($file)");
}
// Write $comment to our opened file.
if (fwrite($handle, $comment) === FALSE) {
die ("Cannot write to file ($file)");
}
echo "Thank you for your comment!";
fclose($handle);
} else {
die( "The file $file is not writable");
}
} else {
die ("The file $file does not exist.");
}
?>