Connecting Tech Pros Worldwide Forums | Help | Site Map

Deleting of a Line from a test file

Newbie
 
Join Date: Mar 2008
Posts: 14
#1: Mar 26 '08
HI,

Well, Here is the code of my new project .. Actually it's just s scrap or it..

I have made 2 files.

the 1st file is test.php with of course creating a new file called testdb.txt.
And the file called admin.php

here is the code.

Code of test.php

[php]
<?php

$db = "testdb.txt";
$name= $_POST["name"];
$like = $_POST["like"];
$submit = $_POST["submit"];
$full_mess = "<br> MY name is $name.<br>I like to $like.";
$seprate = "<br>..............";


if($submit == submit)
{
$f_handle = fopen($db,'a');
fwrite($f_handle,$full_mess.$seprate."\n");
fclose($f_handle);
"<i>Message sent...";
}
else
{
echo "Message Can't be sent...";
}
?>

<form name="beta_testing" action="test.php" method="post">
<br>
Name ; <input name="name" type="text" size="18">
<br>
Like ; <input name="like" type="test" size="20">
<br>
<input name="submit" type="submit" value="submit">
</form>

<?php

@include($db);
?>
[/php]

and admin.php

[php]
<?php

$db = "testdb.txt";
$pass = $_POST["pass"];
$line_to_del = $_POST["line"];
$log_in_form = "<form name=\"moderation_login\" action=\"admin.php\" method=\"post\">
<br>
Please insert your password to get in...<input name=\"pass\" type=\"text\" size=\"15\">
<input name\"submit\" type=\"submit\" value=\"submit\">
</form>";

$lines_del_form = "<form name=\"moderation\" action=\"admin.php\" method=\"post\">
<br>
Line Number to delete.. Lines are counted from Down to up...<input name=\"line\" type=\"text\" size=\"15\">
<input name\"submit\" type=\"submit\" value=\"submit\">
</form>";

if($pass == 123)
{
@include($db);
$file = file_get_contents($dbname);

$array = explode("\n", $file);

array_diff(array($array), array($line_to_del));

echo $lines_del_form;
}else
{
echo "<b> Please insert correct value into pass field...";

echo $log_in_form;
}

?>

[/php]

Now, When a member/Dude inserts the info that his/her name with Likeness.
It will be submitted, But will be written the info of each dude as 1 line..

Now if suppose five people have inserted and i want to delete the info of 3rd member (3rd line) from testdb.txt..

How can i>..

Help please :)

FAISAL!

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#2: Mar 26 '08

re: Deleting of a Line from a test file


I recommend that when you write the information, have a separator of some kind, like ; or something so your file will have:

Person_1 ; Person_2 ; Person_3 ...

Rewrite your document as a variable... say $doctoedit

So now if you want to delete person 2:
[PHP]$doctoedit = str_replace ( 'Person_2 ; ' , '' , $doctoedit );[/PHP]

Well not sure about the coding, but logically that's how I would do it.

[edit]
Forgot it was 1 line per person, but you can do a similar thing with that.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Mar 27 '08

re: Deleting of a Line from a test file


Quote:
....Now if suppose five people have inserted and i want to delete the info of 3rd member (3rd line) from testdb.txt....
Then you will the risk of deleting more than you want. Because:
you could delete line 3 and rewrite the file. The member 4 would become member 3. When the user refreshes the page or 'back-buttons' it will again delete line 3, i.e. the original line 4 and so on.

Why don't you use a user identifier or a simple database instead of accessing / deleting the n.th record in a file?

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 14
#4: Mar 27 '08

re: Deleting of a Line from a test file


Well,
Thanks. But actually before i get into MySQL. I wanted to have some practice on php file writing, reading and database deleting..

therefore i asked for this help..

Well, Can you help me. I TOTALLY agree that it might delete more then I want :(
But actually I want to know it for my practice only.. And I might need it in future :)

Waiting for help :

FAISAL!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#5: Mar 27 '08

re: Deleting of a Line from a test file


Well okay, as long as you understand the risk with this.

Asume you have a text file where ech text line 'normally' ends with a \n.
Then you can read such a file in one command file() into an array: 1 line per array entry, starting at index 0. So when you want to delete a line from that, say 3, you can unset entry [2]. See this little sample.

Text file:[
Expand|Select|Wrap|Line Numbers
  1. 1-abcdefghijk
  2. 2-dfgheritops
  3. 3-nxvvq345qms
  4. 4-jjfjjfjjfjf
PHP script[php]<?php
$line_to_delete=3;
$file=file('test.txt');
unset($file[$line_to_delete-1]);
foreach ($file as $line)
echo "$line<br>";
?>[/php]Result:[
Expand|Select|Wrap|Line Numbers
  1. 1-abcdefghijk
  2. 2-dfgheritops
  3. 4-jjfjjfjjfjf
Ronald
Newbie
 
Join Date: Mar 2008
Posts: 14
#6: Mar 28 '08

re: Deleting of a Line from a test file


Quote:

Originally Posted by ronverdonk

Well okay, as long as you understand the risk with this.

Asume you have a text file where ech text line 'normally' ends with a \n.
Then you can read such a file in one command file() into an array: 1 line per array entry, starting at index 0. So when you want to delete a line from that, say 3, you can unset entry [2]. See this little sample.

Text file:[

Expand|Select|Wrap|Line Numbers
  1. 1-abcdefghijk
  2. 2-dfgheritops
  3. 3-nxvvq345qms
  4. 4-jjfjjfjjfjf
PHP script[php]<?php
$line_to_delete=3;
$file=file('test.txt');
unset($file[$line_to_delete-1]);
foreach ($file as $line)
echo "$line<br>";
?>[/php]Result:[
Expand|Select|Wrap|Line Numbers
  1. 1-abcdefghijk
  2. 2-dfgheritops
  3. 4-jjfjjfjjfjf
Ronald

Thanks Bro.

You really made it so clear and Easy..
Thanks once more....

can you please explain me this bunch of code?
unset($file[$line_to_delete-1]);

FAISAL!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#7: Mar 28 '08

re: Deleting of a Line from a test file


unset() is a command to unset a variable or remove an entry in an array.
You want to delete record number 3, that number is stored in variable $line_to_delete.

The to-be-deleted record no 3 is stored in the array with array index 2 (array starts with index 0, so record 1 is in index 0 and record 3 is in index 2).[php]unset($file[$line_to_delete-1]);[/php]just means:

remove the entry 2 from the array (i.e. record 3).

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 14
#8: Mar 30 '08

re: Deleting of a Line from a test file


Sorry to say :(

BUT I am still stuck with this...

[php]
<?php
//admin.php for deleting entry from a text the system.

$db = "testdb.txt";
$pass = $_POST["pass"];
$line_to_del = $_POST["del"];
$pass_submit = $_POST["pass_submit"];
$entry_del = $_POST["entry_del_submit"];
$log_in_form = "<form method=\"post\" action=\"admin.php\">
<br>
Password > <input name=\"pass\" type=\"text\" size=\"10\">
<input name=\"pass_submit\" type=\"submit\" value=\"pass_submit\">
</form>";

$del_form = "<form method=\"post\" action=\"admin.php\">
<br>
Line Number to delete > <input name=\"del\" type=\"text\" size=\"10\">
<input name=\"entry_del_submit\" type=\"submit\" value=\"entry_del_submit\">
</form>";

if($pass == 123)
{
echo $del_form;
$entry_to_del = $_POST['del'];
$file = file("testdb.txt");
unset($file[$entry_to_del-1]);
foreach($file as $line);
echo $line;
}else
{
"<b>Enter the password to GO!!!</b>";
echo $log_in_form;
}
?>
[/php]

It's the code of my admin.php where I want to delete the lines from an array in the way you told..IN THE BOX, when I put a line number.. NOTHING HAPPEN :(

Help.
FAISAL!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#9: Mar 30 '08

re: Deleting of a Line from a test file


Firstly: the foreach in statement 27 may not be terminated with a semicolumn: it is a block to be executed and this semicolumn terminates it.

Secondly: your code is so badly structured that you can hardly see what belongs where and what executes when. Please indent any code so we can see structure.

Thirdly: you must test what caused (which form post) the invocation of the script. So you must test the $_POST array to see which of the 2 submits was used.

I changed the last part of your code, from line 21, a bit with the checks and now it runs as expected[php]if (isset($_POST['pass_submit']) and $pass == 123) {
echo $del_form;
exit;
}
elseif (isset($_POST['entry_del_submit'])) {
$entry_to_del = $_POST['del'];
$file = file("testdb.txt");
unset($file[$entry_to_del-1]);
foreach($file as $line)
echo "$line<br>";
}
else {
echo "<b>Enter the password to GO!!!</b>";
echo $log_in_form;
}
?>[/php]P.S> I changed the thread title, because the subject of this is a file and not a database.

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 14
#10: Mar 30 '08

re: Deleting of a Line from a test file


Thanks for changing title, as well helping me..

Well, the thing is... that I changed to the code you gave. It logs me in, takes me to $form_del. But the problem here is that I am not able to delete any thing.

When, I put the number of line into del form, and click on Submit, it takes me and shows me the content of MY DATABASE and not deleting even any single line :(

Thanks.. HELP...

The code I am using at this time is ;

[php]
<?php
//admin.php for deleting entry from a text the system.

$db = "testdb.txt";
$pass = $_POST["pass"];
$line_to_del = $_POST["del"];
$pass_submit = $_POST["pass_submit"];
$entry_del = $_POST["entry_del_submit"];
$log_in_form = "<form method=\"post\" action=\"admin.php\">
<br>
Password > <input name=\"pass\" type=\"text\" size=\"10\">
<input name=\"pass_submit\" type=\"submit\" value=\"pass_submit\">
</form>";

$del_form = "<form method=\"post\" action=\"admin.php\">
<br>
Line Number to delete > <input name=\"del\" type=\"text\" size=\"10\">
<input name=\"entry_del_submit\" type=\"submit\" value=\"entry_del_submit\">
</form>";

if (isset($_POST['pass_submit']) and $pass == 123) {

echo $del_form;

exit;

}

elseif (isset($_POST['entry_del_submit'])) {

$entry_to_del = $_POST['del'];

$file = file("testdb.txt");

unset($file[$entry_to_del-1]);

foreach($file as $line)

echo "$line<br>";

}

else {

echo "<b>Enter the password to GO!!!</b>";

echo $log_in_form;
}

?>
[/php]
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#11: Mar 30 '08

re: Deleting of a Line from a test file


Now hold it a second!

Do you expect me to write all the code of this?

I showed you code on how to read a file into a lines array, to delete a record from the lines array. I also showed you that it was deleted by printing out the array after the deletion. Now all you have to do is write the text lines BACK into the file.

You can surely do that without help. Some things you have to do yourself. And for a PHP programmer writing to a file cannot be a problem.

Ronald
Reply