Jump to content

script ignoring lines that don't match, needs a little tweaking


datafan

Recommended Posts

This script allows my users to delete reservations from a flat file by ignoring the lines that match certain variables when it re-writes the file. It works great except that  if the reservations are similar it will delete them, even if all the variables don't match. I must be overlooking something, can you see it? Thanks for any suggestions.

 

Reservation deletion script:

 

<?php
if ($_POST['submit']) {
$file = 'flatfolder/masterflat.txt';
$newfile = 'flatfolder/masterflat.txt.bak';
	if (!copy($file, $newfile)) {
		echo "failed to copy $file...\n";
	} else {
		$delete_msg = "File copied successfully";
	}	

$class_name = $_POST['class_name'];
$classfilename = $_POST['classfilename'];
$keyword = $_POST['keyword'];
$filename = "flatfolder/masterflat.txt";
$file_array = file($filename);

foreach ($file_array as $file_line) {
	$file_lines = explode("|", trim($file_line));

	if ($file_lines[4] != $classfilename && $file_lines[6] != $keyword && $file_lines[7] != $class_name) {
		$new_file_array[] = implode("|", $file_lines);
	}
}

$open_file = fopen($filename, 'w');
flock ($open_file, LOCK_EX);
fwrite($open_file, implode("\n", $new_file_array)."\n");
flock ($open_file, LOCK_UN);
fclose($open_file);
unlink("reservations/$classfilename");
echo "The reservation has been removed successfully.<br />";
}
?>

 

Flat file example:


1210568400|c5|0800|1700|05-12~05-14c5800-500.txt|no_wall_moves|1003|COE LSS Class
1210654800|c5|0800|1700|05-12~05-14c5800-500.txt|no_wall_moves|1003|COE LSS Class
1210741200|c5|0800|1700|05-12~05-14c5800-500.txt|no_wall_moves|1003|COE LSS Class
1214197200|c3|0800|1700|06-23~06-23c3800-500.txt|no_wall_moves|1004|jim'a class
1214197200|c2|0800|1700|06-23~06-23c2800-500.txt|no_wall_moves|1005|jim'a class

 

So if i fill out the form with ($classfilename)06-23~06-23c2800-500.txt    ($keyword)1004  ($class_name)jim'a class 

 

It will delete both the "jim'a class" entries at the end even though one of them has ($keyword)1005 

 

Is it possible that the ~ and - in the file names (example: 06-23~06-23c2800-500.txt) are messing up the other comparisons somehow? How can the $keyword clearly not match but yet that entire line is also removed from the flat file? These are computers, that is not suppose to happen :-)   

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.