se1zure Posted January 20, 2007 Share Posted January 20, 2007 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 Quote Link to comment Share on other sites More sharing options...
fert Posted January 20, 2007 Share Posted January 20, 2007 http://us3.php.net/manual/en/function.copy.phphttp://us3.php.net/manual/en/function.unlink.php Quote Link to comment Share on other sites More sharing options...
Crimpage Posted January 20, 2007 Share Posted January 20, 2007 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)NameEmailMessageDrop all the shouts in there then when you select them, just do:Select * from shoutbox order by id desc limit 10That 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 Quote Link to comment Share on other sites More sharing options...
se1zure Posted January 20, 2007 Author Share Posted January 20, 2007 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 formif ($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 countfclose($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 messagefclose($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 messagefclose($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. Quote Link to comment Share on other sites More sharing options...
fert Posted January 20, 2007 Share Posted January 20, 2007 If you're using Linux the text editor might have syntax coloring Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 Textpad *points to link in signature* Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.