Jump to content

Deleting the first line when submitting message


Luigi987

Recommended Posts

Hi,

I need some way to delete the first line from a text file while submitting a message,

29dfk28.png

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 !\"$%^&*()<>

 

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.

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-

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.