View Full Version : delete marked line in an included file
motormichael12
11-23-2006, 07:04 PM
Okay I have a script that I want to set up to where you can set the string to be deleted by a checked checkbox next to it, so that it will delete it from the file and not delete everything in the file.
<?
$dataf = "posts.txt";
$name = $_POST['name'];
$message = $_POST['message'];
if ($message != "") {$file = fopen("$dataf","a");
$write = fwrite($file," ·· <a href=$name>$message</a>");
fclose($file);
}
?>
<br>
<? include("posts.txt"); ?>
<form name="add" action="" method="post">
Locate: <input name="name" size="50" type="text" id="name">
Text: <input name="message" size= "50" type="text" id="message">
<input type="submit" name="Submit" value="Submit"><input type="reset" name="Reset" value="Reset">
</form></td>
</tr>
</table>
I want it to have another button next to reset that is named as delete and when it saves files to the .txt file it will add a checkbox in front of it, but then you can click on the checkbox and it will delete what is checked when you click delete button.
If that made any sense then please help me
This is why form element arrays exist.
<?php
$thefile = 'your_file.conf';
function deleteLine($file, $lineNum) {
$c = file($file);
$c[$lineNum] = '';
$c = implode('', $c);
$f = fopen($file, 'w');
fwrite($f, $c);
fclose($f);
}
if(isset($_POST['lines']))
for($i = 0; $i < count($_POST['lines']); ++$i)
deleteLine($thefile, $_POST['lines'][$i]);
$c = file($thefile);
?>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<?php for($i = 0; $i < count($c); ++$i) { ?>
<label style="display: block;">
<input type="checkbox" name="lines[]" value="<?php echo($i); ?>">
<?php echo($c[$i]); ?>
</label>
<?php } ?>
<input type="submit" value="Delete these lines">
</form>
motormichael12
11-23-2006, 09:19 PM
can i change the file extension or does it have to be a conf file?
motormichael12
11-23-2006, 10:26 PM
okay, I combined it wiht my otyher script and now it works except one things...
If you have any checked then it will delete that even if you don't click "delete", such as adding something. Can someone tell me the script to set each button to do a function?
<?php
$thefile = 'posts.txt';
$dataf = 'posts.txt';
$name = $_POST['name'];
$message = $_POST['message'];
function deleteLine($file, $lineNum) {
$c = file($file);
$c[$lineNum] = '';
$c = implode('', $c);
$f = fopen($file, 'w');
fwrite($f, $c);
fclose($f);
}
if ($message != "") {$file = fopen("$dataf","a");
$write = fwrite($file,"text inserted here
");
fclose($file);
}
if(isset($_POST['lines']))
for($i = 0; $i < count($_POST['lines']); ++$i)
deleteLine($thefile, $_POST['lines'][$i]);
$c = file($thefile);
?>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<?php for($i = 0; $i < count($c); ++$i) { ?>
<label style="display: block;">
<input type="checkbox" name="lines[]" value="<?php echo($i); ?>">
<?php echo($c[$i]); ?>
</label>
<?php } ?>
Link Text: <input name="message" size= "30" type="text" id="message">
URL: <input name="name" size="30" type="text" id="name">
<input type="submit" name="Submit" value="Submit"><input type="reset" name="Reset" value="Reset"><input type="submit" value="Delete these lines">
</form>
i want the red to be used when they click on "delete" and the blue to happen when they click on the "submit" button
if(isset($_POST['lines']) && isset($_POST['del']))
for($i = 0; $i < count($_POST['lines']); ++$i)
deleteLine($thefile, $_POST['lines'][$i]);
<input type="submit" value="Delete these lines" name="del">
motormichael12
11-23-2006, 11:17 PM
that leaves a blank checkbox there taht I don't want.
motormichael12
11-23-2006, 11:29 PM
I fixed that problem but now there is a problem that was in it since the beginning, I havent said anything about it though.
If you make 5 lines so that there is:
(checkbox) 1
(checkbox) 2
(checkbox) 3
(checkbox) 4
(checkbox) 5
then check all of them and delete, for some reason it only deletes 1, 3, and 5. Then if you try to delete 2 and 4 at the same time it only deletes 2.
After more tests (using # 1-11 and it deleting all odds) I have concluded...
Its only deleting every other one. Can you help me fix this?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.