jdm95lude Posted March 15, 2008 Share Posted March 15, 2008 I know that this isnt the right way to go about it a database would be much more realistic but its an assignment. Its a telephone directory that you search for a First Name and Last Name if its there it takes you to an update.php page which there you can edit it and it wrights it back to the txt file. If it doesn't exists you can go to add an entry which then wrights a new entry to the end of the txt file. I got all of it working except when I have it "update" to the txt file it updates the correct entry but deletes the rest of the entries in the txt file. Here is the link to the programm. Please lend a hand here I've been trying to figure it out all day. Thanks. http://apollo1.occc.edu/sp0808/ssp_05/directory/directory.php Here is the code for the UpdateRecord.php <?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");?> Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/ Share on other sites More sharing options...
jdm95lude Posted March 15, 2008 Author Share Posted March 15, 2008 anyone... Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/#findComment-492699 Share on other sites More sharing options...
johnny44 Posted March 15, 2008 Share Posted March 15, 2008 Check your variable in your forloop. Are you working with $x or $i ? Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/#findComment-492766 Share on other sites More sharing options...
jdm95lude Posted March 15, 2008 Author Share Posted March 15, 2008 yeah I changed it to $x and it messed it all up. I don't know why but when i use diff variables it works some what. Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/#findComment-493068 Share on other sites More sharing options...
discomatt Posted March 15, 2008 Share Posted March 15, 2008 Your script can be simplified quite a bit. I'm not sure why you use fopen()/fread() method, and then use file_put_contents() afterwards. Here's an easier way: <?php $c = file('directory.txt'); foreach ($c as $line) { $line = rtrim($line); $UpdatingNameSearch = explode(", ", $line); if ((strcasecmp($FName, $UpdatingNameSearch[0]) == 0) && (strcasecmp($LName, $UpdatingNameSearch[1]) == 0)) { $line = strtolower("$FName, $LName, $Address, $City, $State, $Zip, $Phone"); $update = 1; } $newString .= $line . "\r\n"; } if ($update == 1) file_put_contents($newContent); ?> Haven't tested it, but you get the general idea Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/#findComment-493075 Share on other sites More sharing options...
jdm95lude Posted March 16, 2008 Author Share Posted March 16, 2008 where are you getting $newContent and file_put_content has to have two parameters. Link to comment https://forums.phpfreaks.com/topic/96247-files-and-directories-using-txt-files/#findComment-493626 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.