Jump to content

Files and Directories using txt files


jdm95lude

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.