<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="author" content="Roary Tubbs" />
<meta name="Apollo1Account" content="sp0808" />
<meta name="revised" content="03-11-2007" />
<meta name="filename" content="UpdateRecord.php" />
<meta name="description" content="Assignment Telephone Directory" />
<meta name="keywords" content="Server Side Programming" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<title>Server-side Programming: Assignment 5 - Telephone Directory: Roary Tubbs</title>
<link rel="stylesheet" href="scripts/directory.css" type="text/css" />
</head>
<body>
<?php include("header.php");?>
<div class="table">
<table width="525" class="spacer" >
<tr><td>
<div class="spacer2"></div>
<?php
// Variables that GET data from AddForm.php
$FName = $_POST['FName'];
$LName = $_POST['LName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$Phone = $_POST['Phone'];
$Flag = FALSE;
$DirectoryName = "directory.txt";
// Asign txt file to a variable and opens it for reading and wrighting
$DirectoryFile = fopen("directory.txt", "r+");
//gets first line of the directory
$NameSearch = fgets($DirectoryFile);
//goes through the directory until the end of file
while (!feof($DirectoryFile))
{
//writes current line into an array
$UpdatingNameSearch = explode(", ", $NameSearch);
//does string compare for user name, if found then writes the new user information and sets off the flag
if ((strcasecmp($FName, $UpdatingNameSearch[0]) == 0) && (strcasecmp($LName, $UpdatingNameSearch[1]) == 0))
{
$UpdatedInfo = "$FName, $LName, $Address, $City, $State, $Zip, $Phone, \\r\\n\r\n";
$UpdatedInfo = strtolower($UpdatedInfo);
$NewContent[$Count] = $UpdatedInfo;
//else if user not found, writes the current line to an array for rewriting to txt file later
} else {
$NewContent[$Count] = "$NameSearch";
}
//increments counter for new content array
++$Count;
//gets next line of txt file
$NameSearch = fgets($DirectoryFile);
}
//for loop to write all the data back in order to the txt file
for ($x = 0; $x < count($NewContent); ++$x)
{
//if at the begining of the file, overwrites all data in it with the first line of txt and sets off an error flag if it doesn't work
if ($i == 0)
{
if (file_put_contents($DirectoryName, $NewContent[$i]) <= 0 )
{
$Flag = TRUE;
}
} else {
//if not the first line then appends the information and sets off flag if there is an error
if (file_put_contents($DirectoryName, $NewContent[$i], FILE_APPEND) <= 0 )
{
$Flag = TRUE;
}
}
}
//if no error displays success message
if ($Flag != TRUE)
{
echo "<div class='content2'>Update Successful:</div>";
echo "<p class='special'>Entry has been successfully updated to the directory!</p>";
echo "<p class='content'>First Name: $FName <br />";
echo "Last Name: $LName <br />";
echo "Address: $Address <br />";
echo "City: $City <br />";
echo "State: $State <br />";
echo "Zip: $Zip <br />";
echo "Phone: $Phone</p>";
echo "<div class='return'><a href='directory.php'>Return to Directory</a></div>";
} else {
//if error, displays error message
echo "<div class='content2'>Error# 37342</div>";
echo "<div class='special'>There has been an Eror. Please go to the Add Entry page to add this information.</div>";
echo "<div class='return'><a href='AddForm.php'>Return to Add Entry</a></div>";
}
// Closes txt file
fclose($DirectoryFile);
?>
<div class="spacer2"></div>
</td></tr>
</table></div>
<?php include("footer.php");?>
</body>
</html>