Jump to content

Making ShoutBox : Need a little help.


se1zure

Recommended Posts

Hi Everyone. I am making a simple PHP shout box, which writes to a text file. My problem is, that the text file can have unlimited posts, (it writes to it using the a+ method).

My idea is to have a counter going, after each submitted posts, for example, create the var:
$postcount = 0;

and then, have each post use this:
$postcount++;

and :

if ($postcount==10)
{ //CODE THAT WILL CLEAR THE TEXT FILE, then continue and post the new comment.}
else
// continue posting the comment.


Basically, I need help with code that will clear a text file, and, if possible, backup that data to a continually running separate text file.

Any help will be appriciated
Link to comment
Share on other sites

Do you want it to do that so you never get more than 10 shouts?

The problem with that is you are going to need to store the post count somewhere too. So you will need to have a text file for the shouts, and a text file that holds the current post count, and a text file with all the archived shouts...

1 Database table would fix all that.
ID (Pri_key)
Name
Email
Message

Drop all the shouts in there then when you select them, just do:

Select * from shoutbox order by id desc limit 10

That way you will always get the 10 most recent shouts, no worries about counting current postcount or archiving the old ones because they just never get selected anymore.

I haven't had much to do with writing to text files, so if there is a reason why you need to run with them, then the above will help.
Hope it helped.

Cheers,
Dave
Link to comment
Share on other sites

Well, I'm still new to php. I just started learning yesterday, but luckily I have a little java background.

I havent gottento mysql databases yet (I am following an online Guide to PHP, and mysql comes up later).

Here is what i am trying right now (I know, it's messy... Im a noob):

[code]
<?php
$name = $_POST["name"]; // gets name from form
$message = $_POST["message"]; // gets message form form

if ($message == " " || $name == "" || $message == "")//detecs spam
    $post = "";
else { // if the message and name are filled in, proceed.
$filename= "counter.txt" ; // Get postcount text file
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ; //add to post count
fclose($fd) ;
if ($filename<=10){ // check if post count is < or = to 10
$post = "<b>$name:</b><br \> $message <hr NOSHADE>"; // declare post
$myFile = "shouts.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "$post";
fwrite($fh, $stringData); // Post the entire message
fclose($fh);
  }
else // If counter.txt HAS reached 10.
{$myFile = "shouts.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "";
fwrite($fh, $stringData); // Delete all current posts by writing w/ (w)
fclose($fh);

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring = 1 ;
$fout= fwrite ($fd , $fcounted ) ; // Reset counter to 1.
fclose($fd) ;

$myFile = "shouts.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "$post";
fwrite($fh, $stringData); // Post the entire message
fclose($fh);

}
}

?>[/code]

Right now, it adds to the counter.txt file, but for some reason, when it gets to ten, it doens't delete the posts, and restart the counter, it keeps counting, and adding to the posts.

EDIT: Whoah! Anyone know if there is a program that color codes my java code , like the box above? I use eclipse for java, but I'm not usre if there is anyithing for php.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.