session_name("cims");
session_start();
if ($_GET['remove'] != "") {
remove_line_from_file($_GET['remove']);
} else {
write_to_file();
}
?>
Simple Form
Data File Contains
display_file_contents() ?>
// ****** FUNCTION ******
// NAME: write_to_file
// PARAMETERS: none
// PURPOSE: This function writes the contents of the $_GET
// variable database. If the file "data.txt" does not
// already exist, create it automatically.
function write_to_file() {
$filename = "data.txt";
$dir = "./";
$string = $_GET["Name"] . "," . $_GET["Hobby"] . "\n";
if ($hFile = fopen($dir . $filename, "a+")) {
// file opened successfully so continue
fwrite($hFile, $string);
//print " Wrote to file.";
fclose($hFile);
} else {
print "
Error. Couldn't write to file!";
// file could not be opened, so just don't do anything
}
}
// ****** FUNCTION ******
// NAME: display_file_contents
// PARAMETERS: none
// PURPOSE: read all the data from the database and print it out in html
function display_file_contents() {
$filename = "data.txt";
$dir = "./";
$contents = file_get_contents($dir . $filename);
$contents = explode("\n", $contents);
print "
";
print "| # | Name | Hobby | ";
$cnt = 0;
foreach ($contents as $line) {
if ($line != "") {
$line = explode(",", $line);
print " |
";
print "| " . ($cnt + 1);
for ($i = 0; $i < 2; $i++) {
print " | " . $line[$i];
}
print " | ";
$cnt++;
}
}
print " |
";
}
function remove_line_from_file($line_to_remove) {
$filename = "data.txt";
$dir = "./";
if (file_exists($dir . $filename)) {
// file exists can attempt to remove line
$contents = explode("\n", file_get_contents($dir . $filename));
if ($hFile = fopen($dir . $filename, "w")) {
// opened file, do the deed
for ($i = 0; $i < sizeof($contents); $i++) {
if ($i != $line_to_remove) {
fwrite($hFile, $contents[$i]);
if ($i < sizeof($contents) - 1) {
fwrite($hFile, "\n");
}
}
}
} else {
// couldn't open file, there was a problem
}
}
}
?>