473,473 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Deleting of a Line from a test file

14 New Member
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!
Mar 26 '08 #1
10 1722
TheServant
1,168 Recognized Expert Top Contributor
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.
Mar 26 '08 #2
ronverdonk
4,258 Recognized Expert Specialist
....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
Mar 27 '08 #3
Faisal Shah
14 New Member
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!
Mar 27 '08 #4
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 27 '08 #5
Faisal Shah
14 New Member
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!
Mar 28 '08 #6
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 28 '08 #7
Faisal Shah
14 New Member
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!
Mar 30 '08 #8
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 30 '08 #9
Faisal Shah
14 New Member
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]
Mar 30 '08 #10
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 30 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
6
by: Abhijeet | last post by:
I was just toying around idea of deleting this from a member function. Was expecting that any acess to member variable or function after deleting sould give me dump(segmetation violation).Cause now...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
2
by: angus | last post by:
hello everybody, i'm having some problems searching a text file and then deleting the line. since i used fileopen(), i can use the eof() function to loop through the file, searching for the...
5
by: Joe Delphi | last post by:
Hi Newbie to VB.Net and I have a question I need to open a text file, read each line, and if I find something in the line, delete that line from the text file. Can anyone tell me how to do...
46
by: DP | last post by:
hi, i've got a form, with a subform in it. i've got a delete button in the subform. the code i;ve got is; Private Sub cmdDeleteRecord_Click() msg = "Are you sure you want to delete this...
14
by: micklee74 | last post by:
hi say i have a text file line1 line2 line3 line4 line5 line6 abc
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.