0

I have been trying to get this directory to add data into an associative array for an assignment I am working on but I keep getting useless results no matter what i try and so I thought I would check here for advice. This is the HTML form that is used to collect the data from the user:

<!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>

and the data collected by this form is supposed to be stored in an array for future retrieval and manipulation by this PHP script:

<!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>
<a href="Lloyd_assignment4.html>Back to directory</a>
<?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>

but although the output does include the entered information and a link back to the HTML entry form it does not seem to save the data into the array and whenever you run the entry form the new data overwrites the previous entry and so you get an array with only one value which is the most recently entered data!!:icon_sad:
What I need is a way to store each individual entry as a string in an associative array to be manipulated and printed on screen when accessed!!
Can someone please tell me what I am missing here?? how can I keep from overwriting the array each time the entry form is used??:icon_question:
I know I need to have a way to advance the internal array pointer but I am not sure how exactly and any advice would be golden!!:icon_cheesygrin:
PLEASE HELP ME!!!!
zlloyd1

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by zlloyd1
0

yes,
you can do this by using sessions...

try this:

session_start();
if(!isset($_SESSION['words']))
{
   session_register();
   $_SESSION['words']=array();
}
$iterator=count($_SESSION['words']);
$_SESSION['words'][$iterator][] = wordCheck($lname, "lastname");
$_SESSION['words'][$iterator][] = wordCheck($addr, "address");
.
.
.
.
print_r($_SESSION['words']);
0

yes,
you can do this by using sessions...

try this:

session_start();
if(!isset($_SESSION['words']))
{
   session_register();
   $_SESSION['words']=array();
}
$iterator=count($_SESSION['words']);
$_SESSION['words'][$iterator][] = wordCheck($lname, "lastname");
$_SESSION['words'][$iterator][] = wordCheck($addr, "address");
.
.
.
.
print_r($_SESSION['words']);

Thank you a million times that really helped!!:icon_smile:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.