Click to See Complete Forum and Search --> : problems with text based message board


Bozebo
08-27-2005, 06:37 PM
ok i made a simple text based tag board style thingy with smileys and it workes quite well but there is one major problem

lets get our heads rought what im trying to do shall we---
i have 5 files.
tags.txt where the php script's outputs are stored and then read as html and displayed as the posts

board.php this is the user interface, it has a frame with a file displaying the posts. it also has the html form that sends of the data to the php script

post.php this is the one that i dont think is working right. it recieves the data from the form on board.php and runs htmlspecialchars and the smiley function i made using arrays and writes the output to the txt file

smileylist.php this has nothign to do with the actual proccess of the script it just shows the smileys and their commands in an html table

messages.php this file may not be nesesary atm but i added it when i was first trying out the smileys. but theres no harm in keeping it, it may have a slight problem though


ok ok enough babling on, heres the code in post.php

<?php
function smilies($text){
$smilies['plaintext'] = array(":)",":(",":@",":o",":s",":|");
$smilies['htmltext'] = array("<img src='smilies/smile.GIF'>","<img src='smilies/unhappy.GIF'>","<img src='smilies/angry.GIF'>","<img src='smilies/shocked.GIF'>","<img src='smilies/confused.GIF'>","<img src='smilies/ok.GIF'>");
$text = str_replace($smilies['plaintext'],$smilies['htmltext'],$text);
return $text;
}

$name = htmlspecialchars($_POST['name']);
$messagesmiley = htmlspecialchars($_POST['message']);

$message = smilies("$messagesmiley");

$tags = fopen("tags.txt", "r+");

$post = "<tr><td colspan=2 bgcolor=green>Name: $name</td></tr><tr><td bgcolor=grey valign=top>Message:</td><td bgcolor=grey> $message<br><br></td></tr>";

fwrite($tags, $post);

fclose($tags);

header("location: board.php");
?>


ok, the problem i have is that it writes over the previous posts, so it efeectivly truncates the file only r+ is specifically designed not to. im not sure why its not working. it only fwrites the post that the user entered

heres my other problem which may be on messages.php
it always outputs the new post and if it is smaller than the previous one then it has whats left of the old post stuck above it usually something like tr>. and its annoying. i think it is having the same affect as you get if you press insert and every new character overwrites the old one.
heres the link of the file board.php take a look at my problem
This is the board (http://www.boz.trotinter.net/learning/message%20board/board.php)

feel free to test it


ty in advanced. i would like to get this workign so i can put it up on my site

NogDog
08-27-2005, 07:19 PM
Use 'a' for fopen() if you just want to append to the end of the file, use 'a+' if you want to open the file for both reading and appending.

Bozebo
08-28-2005, 02:04 AM
k il try that

Bozebo
08-28-2005, 10:01 AM
k now i get these errors
Warning: fopen(tags.txt): failed to open stream: Success in /home/boztrot/public_html/learning/message board/post.php on line 14

Warning: fwrite(): supplied argument is not a valid stream resource in /home/boztrot/public_html/learning/message board/post.php on line 18

Warning: fclose(): supplied argument is not a valid stream resource in /home/boztrot/public_html/learning/message board/post.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/boztrot/public_html/learning/message board/post.php:14) in /home/boztrot/public_html/learning/message board/post.php on line 22

and i was wondering, there is no n and n+ i checked on http://www.php.net/fopen

NogDog
08-28-2005, 10:40 AM
...

and i was wondering, there is no n and n+ i checked on http://www.php.net/fopen
I wrote 'a' and 'a+'.

Bozebo
08-28-2005, 11:12 AM
both?????

w00t now i really am confused

Bozebo
08-28-2005, 11:36 AM
well because this topic has a few posts its prolly not gonna b ppls priority to have a look at and help me :( even though im not any better off than i was when i made it lol

NogDog
08-28-2005, 12:56 PM
Read this again carefully: http://www.php.net/fopen

I never made any mention of using an 'n' or 'n+' parameter. You want to append data to the end of a file, therefore you want to use either 'a' or 'a+' as the second parameter to fopen(). Since you are not reading anything from the file after you do your fopen(), just use the 'a' parameter:

if($tags = fopen("tags.txt", "a")
{
$post = "<tr><td colspan=2 bgcolor=green>Name: $name</td></tr><tr>" .
"<td bgcolor=grey valign=top>Message:</td>" .
"<td bgcolor=grey>$message<br><br></td></tr>\n";
fwrite($tags, $post) or die("ERROR: unable to append to file.");
fclose($tags);
}
else
{
die("ERROR: unable to open file");
}

Bozebo
08-28-2005, 01:28 PM
ok no morw errors, but iv ust gone round in a circle. now i have the new posts at the bottom of the file and i need them at the top, thats why i used r+ at first. so it could place the cursor at the first place in the file

NogDog
08-28-2005, 01:57 PM
Only way you'll be able to do that is to read in the entire file, then recreate it by writing the new entry to the file ("w" for the second parameter of fopen) then writing the rest of the content. Here's one way:

$oldText = file_get_contents("file.txt") or die("ERROR: Unable to read file");
$handle = fopen("file.txt", "w") or die("ERROR: Unable to open file for writing");
$post = "<tr><td colspan=2 bgcolor=green>Name: $name</td></tr><tr>" .
"<td bgcolor=grey valign=top>Message:</td>" .
"<td bgcolor=grey>$message<br><br></td></tr>\n";
fwrite($handle, $post);
fwrite($handle, $oldText);
fclose($handle);