PHP bulletin-type-thing

ethicalkidnap

New Member
Messages
2
Reaction score
0
Points
0
Hey guys, I'm new here, as you can probably tell.
I have a little problem with a certain PHP script.

I'm trying to create a bulletin type system where an admin can enter a message into the given text area and it appears on the front page, but only using text files and the fopen() and fwrite() functions.

The problem I'm having is that I need the newest posts to appear above the older posts, I've done it before but have completely forgotten how I did it xD

So far I have:

PHP:
<?php
$date = date("D M j G:i:s");
$file = 'body.txt';
$inf = "<b>".$date."</b><br />".stripslashes($_POST['index'])."<br /><br />";

!$handle = fopen($file, 'a');
fwrite ($handle, $inf);
echo "Success, wrote text to <a href='index.php'>index</a>";
fclose($handle);
?>

I know the fopen() mode is wrong, and that's part of the problem >.>

I also know I need the pointer to back to the top of the document, and I've tried rewind() and fseek(), but they haven't worked

Does anyone have an idea of how I would go about acheiving what I want to do?
 
Last edited:

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
Right, I'd better get some rep for this :p

To get the pointer to the start of the document, you would use
PHP:
fseek($handle, 0);

I'd use a+ instead of a cause if the file doesn't exist, it'll create it. 'a' puts the file pointer at the very end of the file which is good if you are appending data.

Now to get the part you were asking about, displaying the news in reverse order. This is really easy if you store each news item on a single line.

PHP:
<?php
$data = file('body.txt');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    echo $element;  //Do whatever fancy formatting you want to do here.
}
?>

A preferable approach would be to serialize an array containing your news posts and store THAT in the flatfile instead. Lemme know if you'd like to know bout that and perhaps I'll post up a quick tutorial.
 

ethicalkidnap

New Member
Messages
2
Reaction score
0
Points
0
Aww man, that's exactly what I was looking for!
Thanks a lot!! =D
Enjoy your slightly higher reputation ;D
 
Top