Hello,
I am trying to create a directory of information to be stored in an associative array as strings and I have been having a problem figuring out how to get multiple entries into the array. My entry form (an HTML page) does work and collects the information and sends it to a PHP script which stores it in an array as a string but I need to know how to reset the HTML entry form to add another entry and introduce that to the array as a second value. So far all I have been able to do is enter one set of data and store it into an array. Nevertheless I will show you my code and you can dissect it and make suggestions as to what I might be doing wrong here!!:icon_confused:
First I will show you the code for the HTML entry form:

<!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>Directory Listing</title>
</head>
<body>
<h3>Please enter personal information</h3>
<form action="Directory2b.php" method="post">
<p>Lastname: <input type="text" name="lastname"></p>
<p>Firstname: <input type="text" name="firstname"></p>
<p>Address: <input type="text" name="address"</p>
<p>City: <input type="text" name="city"</p>
<p>State: <input type="text" name="state"></p>
<p>Zipcode: <input type="text" name="zipcode"></p>
<p>Areacode: <input type="text" name="areacode"></p>
<p>Telephone: <input type="text" name="telephone"></p>
<h3>please enter dash in telephone number for our records.</h3>
<input type="reset" value="Clear Form" />&nbsp;
&nbsp;<input type="submit" name="submit" value="send form" />
</form>
</body>
</html>

but as I stated this seems to be working and I simply need to know how to reset this form to gather another set of user data to continue.
This form sends the data to a separate PHP script to store it and eventually manipulate it with the following 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">
<title>Directory</title>
<body>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$lname=$_POST["lastname"];
$fname=$_POST["firstname"];
$addr=$_POST["address"];
$cty=$_POST["city"];
$stat=$_POST["state"];
$zip=$_POST["zipcode"];
$acode=$_POST["areacode"];
$phone=$_POST["telephone"];
$words[] = wordCheck($lname, "lastname");
$words[] = wordCheck($fname, "firstname");
$words[] = wordCheck($addr, "address");
$words[] = wordCheck($cty, "city");
$words[] = wordCheck($stat, "state");
$words[] = wordCheck($zip, "zipcode");
$words[] = wordCheck($acode, "areacode");
$words[] = wordCheck($phone, "telephone");
global $errorCount;
function DisplayError($fieldName, $errorMsg)
{
echo "Error for \"$fieldName\": $errorMsg<br \>\n";
++$errorCount;
}
function wordCheck($data, $fieldName)
{
if (empty($data))
{
DisplayError($fieldName, "Please enter $fieldName");
$retval = "";
}
else
{
return $data;
}
}
if ($errorCount>0) 
{
echo "Please re-enter data.<br />\n";
}
else{
$filename = "file.txt";
$fp = fopen($filename, "w+");
$output =array("$lname , $fname ,  $addr ,  $cty ,  $stat , $zip , $acode - $phone");
}
asort($output);
foreach($output as $directory){

  echo "$directory";
}
?>
</body>
</html>

This does receive the form data and stores it into an array correctly but I need to know how to enter more data from the HTML page because I need a directory with multiple entries that can be manipulated later. :icon_eek:
My project is supposed to create an entry form for a web site that is members only and store the individual data into an associative array for future viewing and manipulations after validating all entry fields.:icon_question:
Please if anyone can I would greatly appreciate any advice on how to do what I am attempting!!:icon_biggrin:

Recommended Answers

All 4 Replies

Your expectation isn't clear and it may not be feasible to do what you are saying you want to do. You have four choices for saving the data:
1. You can save it as an array as you have done. When this program finishes, the array is gone.

2. You can define the array as a session variable. In that case, the array will still be available until the session finishes (when you close your browser).

3. Store the data into one or more files. You opened a file in the program but you don't seem to be saving anything to it. You can use a file as long as there is only one user for the program or if you use a unique file name for each user, or you add some code to serialize the users so only one user can be active at one time.

4. Store the data in a database.

Your initial statement seems to imply that you will be saving multiple files into a directory but maybe that is just a problem with the choice of words. Please clarify exactly what you are trying to accomplish.

Your expectation isn't clear and it may not be feasible to do what you are saying you want to do. You have four choices for saving the data:
1. You can save it as an array as you have done. When this program finishes, the array is gone.

2. You can define the array as a session variable. In that case, the array will still be available until the session finishes (when you close your browser).

3. Store the data into one or more files. You opened a file in the program but you don't seem to be saving anything to it. You can use a file as long as there is only one user for the program or if you use a unique file name for each user, or you add some code to serialize the users so only one user can be active at one time.

4. Store the data in a database.

Your initial statement seems to imply that you will be saving multiple files into a directory but maybe that is just a problem with the choice of words. Please clarify exactly what you are trying to accomplish.

I am a student and this is what I need to do:

Assignments

In this assignment you will continue to develop the telephone directory
application you created in Unit 4. You now have a file with about 10 entries,
containing names, addresses, and phone numbers. In your project for this
week you will create an alphabetized list of the data in the file.

Project

In this assignment you will create an alphabetized list of your telephone
directory. Put a link or button on the telephone directory entry page to
execute this new program.

Create a PHP program to open the file to read the data. You will want to
create an array that contains the name and the phone number. The name
should be constructed so that the last name is first, followed by a comma,
and then the first name. Example: Smith, John

The phone number should contain the area code concatenated to the phone
number. Example: 212-398-0987.

The array will then be sorted in (last) name order using an appropriate PHP
function.

The PHP program will present the data in an HTML table with two columns.
Column headings will label the columns “name” and “phone number.” The
data will be presented in alphabetical order.


//this is where I am having problems!!//**************************************

The response page will also contain a link or button to go back to the HTML
form to add additional phone directory entries. All pages in this application
should have consistent color, headings, titles, and formatting.
I need to know how to link back to the HTML entry form to collect more entries.:icon_eek:
//****************************************************************************


When you have completed this application, you will need to send the folder
that contains the HTML, PHP, and telephone files. You should zip the folder
prior to uploading the file to the Dropbox.


This is due by this Friday and I have only gotten as far as you have seen. If you have any advice on how I could finish this by then I would be eternally in your debt!!:icon_cheesygrin:
For the record what I sent you is basically the directory for Unit 4 that he is talking about except I have the input being saved into an array now and echoed out as such. But I need to be able to save more then one entry into this array so that i will have something to alphabetize for the assignment!!
PLEASE HELP!!!!:icon_confused:

A link back to the entry form could be as simple as:

<a href="entry_form.htm>Back to the entry form</a>

Using whatever you called your initial form module (in place of entry_form.htm).

The description of the assignment assumes that you already have a telephone file with about 10 entries. Based on your questions and code, I don't think that you have even reached that point. It seems that you have some catch-up to do.

On this forum, we'll provide answers to specific questions but it isn't our role to teach you to program or to teach you PHP. That is especially true if you are taking a course that you need to pass on your own merit. Right now I think that you have a problem in building the initial telephone directory and you need to sort out your logic for creating/updating the file and then add some additional PHP code to do it.

A link back to the entry form could be as simple as:

<a href="entry_form.htm>Back to the entry form</a>

Using whatever you called your initial form module (in place of entry_form.htm).

The description of the assignment assumes that you already have a telephone file with about 10 entries. Based on your questions and code, I don't think that you have even reached that point. It seems that you have some catch-up to do.

On this forum, we'll provide answers to specific questions but it isn't our role to teach you to program or to teach you PHP. That is especially true if you are taking a course that you need to pass on your own merit. Right now I think that you have a problem in building the initial telephone directory and you need to sort out your logic for creating/updating the file and then add some additional PHP code to do it.

I did what you suggested and added an href statement to my code but it keeps giving me a parse error on the line I added it.

<!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">
<title>Directory</title>
<body>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$lname=$_POST["lastname"];
$fname=$_POST["firstname"];
$addr=$_POST["address"];
$cty=$_POST["city"];
$stat=$_POST["state"];
$zip=$_POST["zipcode"];
$acode=$_POST["areacode"];
$phone=$_POST["telephone"];
$words[] = wordCheck($lname, "lastname");
$words[] = wordCheck($fname, "firstname");
$words[] = wordCheck($addr, "address");
$words[] = wordCheck($cty, "city");
$words[] = wordCheck($stat, "state");
$words[] = wordCheck($zip, "zipcode");
$words[] = wordCheck($acode, "areacode");
$words[] = wordCheck($phone, "telephone");
global $errorCount;
function DisplayError($fieldName, $errorMsg)
{
echo "Error for \"$fieldName\": $errorMsg<br \>\n";
++$errorCount;
}
function wordCheck($data, $fieldName)
{
if (empty($data))
{
DisplayError($fieldName, "Please enter $fieldName");
$retval = "";
}
else
{
return $data;
}
}
if ($errorCount>0) 
{
echo "Please re-enter data.<br />\n";
}
else{
$filename = "file.txt";
$fp = fopen($filename, "w+");
$output =array("$lname , $fname ,  $addr ,  $cty ,  $stat , $zip , $acode - $phone");
}
asort($output);
foreach($output as $directory){

  echo "$directory";
}
<a href="Lloyd_assignment.html>Back to directory</a>; 
?>
</body>
</html>

but all this does is give me this error:

Parse error: parse error in C:\wamp\www\Directory2b.php on line 56

for the record I do not expect you to do my assignment for me I just wanted some assistance and advice.:icon_redface: It is true that my initial directory did not have multiple entries in it but I did not have the knowledge to return to the entry form then either and for that I thank you but I have done what you said and it still does not function correctly so I am again lost. Perhaps you can see why it is not accepting the the return link in my code!!:icon_question:

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.