Ok, so here is my problem. I'm making a telephone registry that lets a user add information such as first/last name, address, zipcode, state, and telephone number.

Then they save the contact. Which seems to be where my problem is. The contact is saved and their information is posted. A file is created in the directory. HOWEVER for every contact I make it is making a new file for each one instead of appending it to the same file.

Then when I go to do a search by last name of my contacts it will not retrieve any of my contacts that I have saved. Everything else seems to be working correctly. I just can't get the program to put all my contacts in one file, and retrieve and saved contacts. Maybe someone can see where I'm going wrong, any help is much appreciated.

Here is my code:
AddContact.html

<!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>
<title>Main Directory</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<h1>Telephone Directory</h1>
<form action="AddInfo.php" method="post" enctype="application/x-www-form-urlencoded">
<p><b>First Name:</b> <input type="text" name="firstName" size="25" /> <b>Last Name:</b> <input type="text" name="lastName" size="25" /></p>
<p><b>Address:</b> <input type="text" name="address" size="30" /></p>
<p><b>City:</b> <input type="text" name="city" size="25" /> <b>State:</b> <input type="text" name="state" size="2" /> <b>Zipcode:</b> <input type="text" name="zipCode" size="5" /></p>
<p><b>Telephone Number:</b> <input type="text" name="telNum" size="14" /></p>
<p><input type="submit" value="Save Contact"  /><input type="reset" value="Reset Form" /></p>
<p><a href="Contacts.html">Return to the Main Directory page</a></p>
</form>
</body>
</html>

SaveContact.php

<!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>
<title>Save Contact</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$FirstName = addslashes($_POST['firstName']);
$LastName = addslashes($_POST['lastName']);
$Address = addslashes($_POST['address']);
$City = addslashes($_POST['city']);
$State = addslashes($_POST['state']);
$ZipCode = addslashes($_POST['zipCode']);
$TelNumber = addslashes($_POST['telNum']);

if(empty($FirstName) ||
	empty($LastName) ||
	empty($Address) ||
	empty($City) ||
	empty($State) ||
	empty($ZipCode) ||
	empty($TelNumber))
	echo "<hr/><p>You must enter a value in each field.
		  Click your browser's Back button to return to the Directory.</p><hr />";
else if (!is_numeric($ZipCode) ||
	!is_numeric($TelNumber))
	echo "<p>The Zip Code and Telephone Number must contain numeric values! 
		 Click your browser's Back button to return to the Directory.</p>";
else {
	$Contact = $FirstName . "\n";
	$Contact .= $LastName . "\n";
	$Contact .= $Address . "\n";
	$Contact .= $City . "\n";
	$Contact .= $State . "\n";
	$Contact .= $ZipCode . "\n";
	$Contact .= $TelNumber . "\n";
	if (!file_exists("Open"))
		mkdir("Open");
	$ContactFile = fopen("Open\\" . $LastName . ".txt", "w");
if (flock($ContactFile, LOCK_EX)) { 
	(fwrite($ContactFile, $Contact) > 0) {
		$FirstName = stripslashes($_POST['firstName']);
		$LastName = stripslashes($_POST['lastName']);
		$Address = stripslashes($_POST['address']);
		$City = stripslashes($_POST['city']);
		$State = stripslashes($_POST['state']);
		$ZipCode = stripslashes($_POST['zipCode']);
		$TelNumber = stripslashes($_POST['telNum']);
		echo "<h1>Contact Saved</h1>";
		echo "<b>Name:</b> $FirstName $LastName <br />";
		echo "<b>Address:</b> $Address <br />";
		echo "<b>City/State:</b> $City, $State <br />";
		echo "<b>Zip Code:</b> $ZipCode <br />";
		echo "<b>Telephone #:</b> $TelNumber";
		flock($ContactFile, LOCK_UN);
		fclose($ContactFile);
	}
	else
		echo "<p>The Contact could not be saved!</p>";
}
else
	echo "<p>The Contact could not be saved!</p>";
}
?>
<p><a href="Contacts.html">Return to the Main Directory Page</a></p>
</body>
</html>

Contacts.html

<!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>
<title>Contacts</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<h1>Contacts</h1><hr />
<form action="AddNew.html" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="Save Contact" /></p>
</form>
<form action="ViewOpenContacts.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="View Open Contacts" /></p>
</form>
<form action="ViewContact.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value=" View Contact Name " />&nbsp;
<input type="text" name="contactname" /></p>
<p>(Enter an existing Contact by Last Name.)</p>
</form>
</body>
</html>

ViewContact.php

<!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>
<title>View Contact</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_GET['lastName']))
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page.</p><hr />";
else if (is_readable("Open\\" . $_GET['lastName'] . ".txt")) {
	$ContactFields = fopen("Open\\" . $_GET['lastName'] . ".txt", "r");
	$FirstName = stripslashes(fgets($ContactFields));
	$LastName = stripslashes(fgets($ContactFields));
	$Address = stripslashes(fgets($ContactFields));
	$City = stripslashes(fgets($ContactFields));
	$State = stripslashes(fgets($ContactFields));
	$ZipCode = stripslashes(fgets($ContactFields));
	$TelNumber = striplashes(fgets($ContactFields));
	echo "<h1>View Contact</h1>";
	echo "<strong>First Name</strong>: $FirstName<br />";
	echo "<strong>Last Name</strong>: $LastName<br />";
	echo "<strong>Address</strong>: $Address<br />";
	echo "<strong>City</strong>: $City<br />";
	echo "<strong>State</strong>: $State<br />";
	echo "<strong>Zip Code</strong>: $ZipCode<br />";
	echo "<strong>Telephone Number</strong>: $TelNumber";
	fclose($ContactFields);
}
else
	echo "<p>Could not read the Contact!</p>";
?>
</body>
</html>

ViewOpenContacts.php

<!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>
<title>View Open Contacts</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$Dir = "open";
if (is_dir($Dir)) {
	$DirEntries = scandir($Dir);
	foreach ($DirEntries as $Entry) {
		if (strpos($Entry, '.txt') !== FALSE)
			echo $Entry . "<br />";
	}
}
else 
	echo "<p>The Open contact does not exist! You must first san a contact to create the open directory.</p>";
?>
<p><a href="Contacts.html">Return to the Main Directory Page</a></p>
</body>
</html>

I ran all my code through the WDG validator and come up with no errors. Everything seems to work just fine except bringing up the contacts when I search. Been trying to figure this out for days now to no avail :).

The reason you are getting a new file created for each contact is coz of

$ContactFile = fopen("Open\\" . $LastName . ".txt", "w");

For every contact if the lastname is different then it creates a new file. So please hardcode the textfile name.
Also if you want all the contacts to be stored in a single file then you need to change the mode in the fopen() to "a+". so it should be something like this

$ContactFile = fopen("Open\\" . $yourtextfilename . ".txt", "a+");

I also suggest that instead of storing the values in the text file like this

$Contact = $FirstName . "\n";
$Contact .= $LastName . "\n";
$Contact .= $Address . "\n";
$Contact .= $City . "\n";
$Contact .= $State . "\n";
$Contact .= $ZipCode . "\n";
$Contact .= $TelNumber . "\n";

Store the data in one single like with semicolon separated and the new contact details in the newline. Say something like this

$Contact = $FirstName . ";".$LastName . ";".$Address . ";".$City . ";".$State . ";".$ZipCode . ";".$TelNumber . "\r\n";

And when you do a search by last name you can do something like this.

$handle = fopen("Open\\" . $yourtextfilename . ".txt", "r");
while (!feof($handle)) {
    $rowdata = fgets($handle);
    if (isset($Contact_details)) {
    	unset($Contact_details);
    }
    $Contact_details = array();
    $Contact_details = explode(";",$rowdata);
    if ($Contact_details[1] == $LastName_searchstring) {
    	// Contact found
    	break;
    }
}

I hope this helps.

I changed your suggested line of codes and it's still doing the same thing. Even though I changed it from $LastName to $Directory it is still storing each contact in a separate file via their last name.

Which I think is what is causing the issue with the search because it won't bring up any contact information still.

Ok, here's what I had going. My initial AddContact.html was going to an AddInfo.php which I didn't have on my server anymore. For some reason I can NOT get this file to go to SaveContact.php even if I change the form and save it, it insists on going to AddInfo.php.

So what I did to remedy this situation was just copy paste and create AddInfo.php for now just to get things going back on the right track.

If I change the suggested code line from $LastName to $Directory for the file name it gives me an undefined variable error and won't create the file.

If I leave it as $LastName it creates a folder which all the contacts are put into, which I can view the list of .txt files by last name. HOWEVER it still will not pull the information for a contact by last name which isn't making any sense to me.

It has to come back to this page:

<!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>
<title>View Contact</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_GET['lastName']))
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page.</p><hr />";
else if (is_readable("Open\\" . $_GET['lastName'] . ".txt")) {
	$ContactFields = fopen("Open\\" . $_GET['lastName'] . ".txt", "r");
	$FirstName = stripslashes(fgets($ContactFields));
	$LastName = stripslashes(fgets($ContactFields));
	$Address = stripslashes(fgets($ContactFields));
	$City = stripslashes(fgets($ContactFields));
	$State = stripslashes(fgets($ContactFields));
	$ZipCode = stripslashes(fgets($ContactFields));
	$TelNumber = striplashes(fgets($ContactFields));
	echo "<h1>View Contact</h1>";
	echo "<strong>First Name</strong>: $FirstName<br />";
	echo "<strong>Last Name</strong>: $LastName<br />";
	echo "<strong>Address</strong>: $Address<br />";
	echo "<strong>City</strong>: $City<br />";
	echo "<strong>State</strong>: $State<br />";
	echo "<strong>Zip Code</strong>: $ZipCode<br />";
	echo "<strong>Telephone Number</strong>: $TelNumber";
	fclose($ContactFields);
}
else
	echo "<p>Could not read the Contact!</p>";
?>
</body>
</html>

For the life of me though, I can not see why it won't go past the if (empty) clause. No matter what I put in the text box it just goes to that echo statement.

Ok, got it all fixed up to the point of the search again. Finally got the program to write all the contacts to a file called Directory no matter what their last name is so they are all in one spot.

The last name search...still can't get this thing to work.

I changed your suggested line of codes and it's still doing the same thing. Even though I changed it from $LastName to $Directory it is still storing each contact in a separate file via their last name.

Which I think is what is causing the issue with the search because it won't bring up any contact information still.

You got me wrong. Your code obviously creates a new file foreach new lastname. What i meant is this:

$textfilename = "register";
$ContactFile = fopen("Open\\" . $textfilename . ".txt", "a+");

That way you will have one single text file called register.txt in which all the contacts will be stored.
And by stroing each contact details in one line with semicolon separated you can read each line of the register.txt file and thereby the search will also be easy.

Can you post the code you are using to do the search ?

Can you post the code you are using to do the search ?

This is the page that should be doing the search:

<!-- Code for the user to view the contact they searched for by last name -->
<!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>
<title>View Contact</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_GET['lastName'])) //If the text box is empty or has a contacts last name that isn't saved display the error message below
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page.</p><hr />";
else if (is_readable("Contacts\\" . $_GET['lastName'] . ".txt")) { //If the contact is in the directory display the results
	$ContactFields = fopen("Contacts\\" . $_GET['lastName'] . ".txt", "r");
	$FirstName = stripslashes(fgets($ContactFields));
	$LastName = stripslashes(fgets($ContactFields));
	$Address = stripslashes(fgets($ContactFields));
	$City = stripslashes(fgets($ContactFields));
	$State = stripslashes(fgets($ContactFields));
	$ZipCode = stripslashes(fgets($ContactFields));
	$TelNumber = stripslashes(fgets($ContactFields));
	fclose($ContactFields);
	echo "<h1>View Contact</h1>";
	echo "<strong>First Name</strong>: $FirstName<br />";
	echo "<strong>Last Name</strong>: $LastName<br />";
	echo "<strong>Address</strong>: $Address<br />";
	echo "<strong>City</strong>: $City<br />";
	echo "<strong>State</strong>: $State<br />";
	echo "<strong>Zip Code</strong>: $ZipCode<br />";
	echo "<strong>Telephone Number</strong>: $TelNumber";
}
else
	echo "<p>Could not read the Contact!</p>"; //Error Message if contact could not be read
?>
</body>
</html>

Yeah, I realized what I did with the text file after you posted it. Got myself confused then unconfused.

Here on this search page, no matter what I do it just goes to the first echo statement telling me that I need to enter an existing contact. I've tried switching $_GET to $Directory (the file name)...I've tried switching the search to the first name, city , state, zipcode anything I could think of and it just won't grab it for whatever reason.

Ok, atleast I'm getting an error message now. That it could not read the contact.

Here is the page that the script is drawing from:

<!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>
<title>Contacts</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<h1>Contacts</h1><hr />
<!-- Form Code -->
<form action="AddContact.html" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="Save Contact" /></p>
</form>
<form action="ViewContactList.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="View Contact List" /></p>
</form>
<form action="ViewContact.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="View Contact Name" />&nbsp;
<input type="text" name="lastName" /></p>
<p>(Enter an existing Contact by Last Name.)</p>
<!-- End Form Code -->
</form>
</body>
</html>

Ok, got the search working by last name. Finally brings up the contact and all the information.

Still have a couple questions though. I had to go back to creating a file by last name so every contact that has a different name is put into a different file.

Is there a way of doing it so it can still all go into one file called Directory, then draw from that?

ok, instead of using

if (empty($_GET['lastName']))

try this

if (isset($_GET['lastName']) && $_GET['lastName'] != "")

ok, instead of using

if (empty($_GET['lastName']))

try this

if (isset($_GET['lastName']) && $_GET['lastName'] != "")

This for when they are all in one file?

Ok, got the search working by last name. Finally brings up the contact and all the information.

Still have a couple questions though. I had to go back to creating a file by last name so every contact that has a different name is put into a different file.

Is there a way of doing it so it can still all go into one file called Directory, then draw from that?

That is what i have mentioned in my earlier posts.

$filename = "register";
$ContactFile = fopen("Open\\" . $filename . ".txt", "a+");

Please check with the following link to know what happens when you use "a+" mode
http://in3.php.net/manual/en/function.fopen.php

And if at all you finally decide to store the values in the following format which i had already mentioned in my earlier posts

$Contact = $FirstName . ";".$LastName . ";".$Address . ";".$City . ";".$State . ";".$ZipCode . ";".$TelNumber . "\r\n";

Then you need to use the following snippet while searching

$filename = "register";
$handle = fopen("Open\\" . $filename. ".txt", "r");
while (!feof($handle)) {
    $rowdata = fgets($handle);
    if (isset($Contact_details)) {
    	unset($Contact_details);
    }
    $Contact_details = array();
    $Contact_details = explode(";",$rowdata);
    if ($Contact_details[1] == $LastName_searchstring) {
    	// Contact found
       //  $rowdata[0] - > First Name ; $rowdata[1] - > Last Name
    	echo $rowdata[0]." ".$rowdata[1]."<br />";
    }
}

I can't get the search to work, getting a parsing error on this line

$handle = fopen("Open\\" . $filename. ".txt", "r");

At first when I typed it out i was getting all sorts of errors. Such as invalid offset 1 and another one. Got it down to a single parsing error and this is where I'm stuck. I can see this thing in my head but I can't get it. The book I'm going by has nothing like this in the chapter I'm looking at and learning from which is bothering.

Hmmm can you please post the error you are getting ?

Hmmm can you please post the error you are getting ?

Ok, after several attempts I took a step back and started to rewrite the entire directory. What I have now is a page that gets the user information and saves it to a single file. The user then can view the entire directory (which works). They can then save another contact, view the directory, or search by last name.

However the search is still not working correctly. When they search for the contact by last name. It comes up CONTACT FOUND....and here's where the problems start again.

If I try to print out the information of the contact found it only prints out the very first entry in the file no matter what the last name. If they enter a last name that is not in the file I get this error:

Notice: Undefined offset: 25 in C:\wamp\www\SearchContact.php  on line 23

Here is the code for the page that does the search:

<!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>
<title>Main Directory</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$LastName = ($_GET['lastName']); //Sets the user entered data to $LastName
if (empty($LastName)) //if statement to make sure text area is not empty
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page or click the link below.</p><hr />";
else if (isset($LastName)) //else if statement to search for the specific last name.
	$Directory = "directory.txt";
	if(file_exists($Directory)){
		$Contact = file_get_contents($Directory);
		$ContactFields = explode(", ", $Contact);
		for ($i=0; $i<=count($ContactFields); ++$i){
			if ($ContactFields[$i] == $LastName) {
				echo "<h2>Contact Found!</h2>";
					break;
			} 
		}
	}
	else {
		echo "File does not exist!";
	}
?>
<p><a href="ShowDirectory.php">View Directory</a></p>
<p><a href="MainDirectory.html">Return to the Main Directory Page</a></p>
</body>
</html>

I took out the code that prints the contact information because it wasn't working right. Also, if i put an error message such as CONTACT does not exist, it will always go to that even if the contact is found.

Please give the complete details on how you are storing the information in your text file and with atleast 2 or more contacts saved in that file.

Please give the complete details on how you are storing the information in your text file and with atleast 2 or more contacts saved in that file.

Ok. This is how the information is stored in the text file called directory.txt
Joe, Smith, 11 Columbia Avenue, Burgertown, NY, 90909, 5555555555

Here's the .php file that stores the contact:

<!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>
<title>Main Directory</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
if(empty($_GET['firstName']) || //Verifies if the text fields are empty or not
	empty($_GET['lastName']) ||
	empty($_GET['address']) ||
	empty($_GET['city']) ||
	empty($_GET['state']) ||
	empty($_GET['zipCode']) ||
	empty($_GET['telNum']))
	echo "<hr/><p>You must enter a value in each field.
		  Click your browser's Back button to return to the Directory.</p><hr />"; //Statement to tell the user the fields are empty
else if (!is_numeric($_GET['zipCode']) || //Verifies if ZipCode and TelNumber are numeric entries
	!is_numeric($_GET['telNum']))
	echo "<p>The Zip Code and Telephone Number must contain numeric values! 
		 Click your browser's Back button to return to the Directory.</p>"; //Statement to tell user they must enter a numeric entry
else {
	$FirstName = addslashes($_GET['firstName']); //Sets each variable to designated text boxes
	$LastName = addslashes($_GET['lastName']);
	$Address = addslashes($_GET['address']);
	$City = addslashes($_GET['city']);
	$State = addslashes($_GET['state']);
	$ZipCode = addslashes($_GET['zipCode']);
	$TelNumber = addslashes($_GET['telNum']);
	$Directory = fopen("directory.txt", "a"); //Sets the variable to directory.txt data
	if (is_writable("directory.txt")) { //Writes to the file in the designated format
		if (fwrite($Directory, $FirstName . ", " . $LastName . ", " . $Address . ", " 
		. $City . ", " . $State . ", " . $ZipCode . ", " . $TelNumber . "\n"))
		echo "<p><h1>Contact Saved!</h1></p>"; //message to let the user know the save was accepted
		else
			echo "<p>Contact unable to be saved to the $Directory file!</p>"; //error message if contact was not saved.
	}
	else
		echo "<p>Cannot write to the $Directory file.</p>";
	fclose($Directory); //closes the file.
}
?>
<p><a href="MainDirectory.html">Return to the Main Directory Page</a></p>
</body>
</html>

Here's the search code again:

<!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>
<title>Main Directory</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$LastName = ($_GET['lastName']); //Sets the user entered data to $LastName
if (empty($LastName)) //if statement to make sure text area is not empty
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page or click the link below.</p><hr />";
else if (isset($LastName)) //else if statement to search for the specific last name.
	$Directory = "directory.txt";
	if(file_exists($Directory)){
		$Contact = file_get_contents($Directory);
		$ContactFields = explode(", ", $Contact);
		for ($i=0; $i<=count($ContactFields); ++$i){
			if ($ContactFields[$i] == $LastName) {
				echo "<h2>Contact Found!</h2>";
					break;
			} 
		}
	}
	else {
		echo "File does not exist!";
	}
?>
<p><a href="ShowDirectory.php">View Directory</a></p>
<p><a href="MainDirectory.html">Return to the Main Directory Page</a></p>
</body>
</html>

Some things to make your search more efficient.

Since you will be storing many contacts in a single text file, there might be a chance that 2 or more people share the same 'Lastname'. Say for example your text file has the data..

Joe, Smith, 11 Columbia Avenue, Burgertown, NY, 90909, 5555555555
Micheal, Jackson, 11 Columbia Avenue, Burgertown, NY, 90909, 5555555555
Niel, Smith, 11 Columbia Avenue, Burgertown, NY, 90909, 5555555555
Brad, Pitt, 11 Columbia Avenue, Burgertown, NY, 90909, 5555555555

There are 2 people who share the same last name and we will have to display both the contacts in that case, so i will be looping through all the records in the text file to get the matches. Henceforth i have removed the break statement in the while() loop as soon as we find a matched contact. Instead i am storing the 'firstname' and the 'lastname' details into a multidimensional array called '$all_contacts'. You can store more details into this if you want to.

Also i am using fgets() instead of file_get_contents() to read one line at a time from the text file since each line will hold the details of One contact. To know the difference between fgets() and file_get_contents() go through the following links

http://php.net/manual/en/function.fgets.php
http://in3.php.net/manual/en/function.file-get-contents.php

Here goes the code...

<!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>
<title>Main Directory</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$LastName = ($_GET['lastName']); //Sets the user entered data to $LastName
$all_contacts = array(); // This is an array which holds all the matched contacts.
if (empty($LastName)) //if statement to make sure text area is not empty
	echo "<hr /><p>You must enter an existing contact by last name.
		 Click your browser's Back button to return to the Contacts page or click the link below.</p><hr />";
else if (isset($LastName)) {//else if statement to search for the specific last name.
	$Directory = "directory.txt";
	if(file_exists($Directory)){
		$handle = fopen($Directory, "r");
		$ct = 0;
		while (!feof($handle)) {
		    $rowdata = fgets($handle);
		    if (isset($Contact_details)) {
		    	unset($Contact_details);
		    }
		    $Contact_details = array();
		    $Contact_details = explode(",",$rowdata);
		    if (strtolower(trim($Contact_details[1])) == strtolower(trim($LastName))) {
		    	$all_contacts[$ct]['firstname'] = $Contact_details[0];
		    	$all_contacts[$ct]['lastname'] = $Contact_details[1];
		    	$ct++;
		    }
		}
	}
	else {
		echo "File does not exist!";
	}
	if (count($all_contacts) > 0) {
		foreach ($all_contacts as $key => $value) {
			echo $value['firstname']." ".$value['lastname']."<br />";
		}
	} else {
		echo "There were no matches found for your search tag.<br />";
	}
}


?>
<p><a href="ShowDirectory.php">View Directory</a></p>
<p><a href="MainDirectory.html">Return to the Main Directory Page</a></p>
</body>
</html>

Thanks. Sorry for the slow response, I have been very sick and haven't been able to really do anything. I got it fixed, thanks a ton for the help its much appreciated. I will mark this thread as solved. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.