Luigi987 Posted July 5, 2010 Share Posted July 5, 2010 Hi, I need some way to delete the first line from a text file while submitting a message, As you can see that's my comment system, And when too many messages are in there it cuts off as in the text goes down and out of sight. I need a way to auto delete when it gets full or something similar. (Hours of googling and testing resulted in no luck) EDIT, I'm using a flat file database: <luigi45000vr> Test message <Luigi987> Test2 <meta~> Ohai Luigi. Nice signature <luigi45000vr> Ty meta <Guilly> Signature win! <Osama> Ohai luigi <TheEnd35000vr> wow its good dat u can post messages on it <Chris> Hai Luigi! <luigi45000vr> Well, Looks like it works Just need to see if it cuts off right <luigi45000vr> Cuts off right <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> <luigi45000vr> Char test !\"$%^&*()<> Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/ Share on other sites More sharing options...
dezkit Posted July 6, 2010 Share Posted July 6, 2010 Post code please. Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/#findComment-1081735 Share on other sites More sharing options...
Luigi987 Posted July 6, 2010 Author Share Posted July 6, 2010 This is my submission form: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Post to Luigi45000vr's sig!</title> </head> <body bgcolor="#FFFFFF" text="#000000"> <form name="form1" method="post" action="submit.php"> Username: <input type="text" name="user" MAXLENGTH="15"><br>Message: <input type="text" name="msg" MAXLENGTH="50"> <input type="submit" name="post" value="Post!"> </form> </body> </html> This is my submission processor : <?php $username = $_POST['user']; $msg = $_POST['msg']; //the data $data = "<$username> $msg\n"; //open the file and choose the mode $fh = fopen("messages.txt", "a"); fwrite($fh, $data); //close the file fclose($fh); print "Message successfully posted!"; ?> Something that removes the first line when it reaches the limit would be what it needs but I'm not sure how to do that. Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/#findComment-1081885 Share on other sites More sharing options...
ChemicalBliss Posted July 6, 2010 Share Posted July 6, 2010 Ok i think the easiest way is to: 1. Get the whole DB file. 2. Remove the first line (if lines is >= max lines) 3. Add line to end from user. 4. delete whole DB file 5. recreate new whole DB file with modifications. eg: 1. use file() to get the DB contents in an array of lines. 2. Count() the number of lines - you can just use count() on the file() result as its already an array of lines. 3. if the count() is >= say 10, then unset() the first element from the file() function. 4. Add new element to end of file() array then implode() to make into a single string. 5. unlink() the original DB file 6. fopen() with a+ a new DB file with same name of course. voila! Come back if you get stuck somewhere in there. -cb- Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/#findComment-1081893 Share on other sites More sharing options...
Luigi987 Posted July 6, 2010 Author Share Posted July 6, 2010 Can you give me an example of it in code? (sorry I'm semi new to php) Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/#findComment-1081931 Share on other sites More sharing options...
ChemicalBliss Posted July 6, 2010 Share Posted July 6, 2010 Start coding it as I outlined above. Any functions I've mentioned just type into php.net. eg: www.php.net/file When you get an array you can remove the first element by two ways: array_shift(); which can be useful if you want a log of what was removed. or just an unset($array[0]); There are also two ways you can add an element to the end of an array: array_push(); which also returns the number of elements in the modified array. or just $array[] = "new item"; -cb- If you get stuck, show us how far you got. Link to comment https://forums.phpfreaks.com/topic/206816-deleting-the-first-line-when-submitting-message/#findComment-1081940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.