/    Sign up×
Community /Pin to ProfileBookmark

problems with text based message board

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

[code=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”);
?>
[/code]

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
[URL=http://www.boz.trotinter.net/learning/message%20board/board.php]This is the board[/URL]

feel free to test it

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

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 28.2005 — 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.
Copy linkTweet thisAlerts:
@BozeboauthorAug 28.2005 — k il try that
Copy linkTweet thisAlerts:
@BozeboauthorAug 28.2005 — 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
Copy linkTweet thisAlerts:
@NogDogAug 28.2005 — ...

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

I wrote [SIZE=6]'a'[/SIZE] and [SIZE=6]'a+'[/SIZE].
Copy linkTweet thisAlerts:
@BozeboauthorAug 28.2005 — both?????

w00t now i really am confused
Copy linkTweet thisAlerts:
@BozeboauthorAug 28.2005 — 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
Copy linkTweet thisAlerts:
@NogDogAug 28.2005 — 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:
[code=php]
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");
}
[/code]
Copy linkTweet thisAlerts:
@BozeboauthorAug 28.2005 — 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
Copy linkTweet thisAlerts:
@NogDogAug 28.2005 — 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:
[code=php]
$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);
[/code]
×

Success!

Help @Bozebo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...