0

Hello, I am supposed to alphabetize an array created from an HTML entry form. The array is supposed to take the information entered and place it into individual strings which will them be placed into an array which I must then alphabetize by last names. This is the code for my HTML entry 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>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 this works fine, the data is entered and transferred to the PHP file for processing. This is the PHP file 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");
ksort($output);
foreach ($output as $directory)
{
    echo "$directory";
}
?>
</body>
</html>

and this is where I am having issues. You see I would like to introduce both blank spaces in between certain fields and place a - between the areacode and the phone number. Unfortunately every time I try to add anything to the array I get unpredictable results when printing out the results. :icon_mad:
Anyone who could point me in the right direction would be a savior and I would be eternally grateful.:icon_biggrin:
Also I would appreciate any suggestions of how to alphabetize the entered data once it is formatted correctly. I am fairly new to PHP scripting.:icon_redface:
I will await any replies hopefully!!
Zack:icon_question:

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

Hi Zack,

as you can see, declaring array must be like this

$output =array($lname, $fname , $addr, $cty, $stat, $zip, $acode, $phone);
you should not put "" on between the whole expression. like what you've did:
$output =array("$lname, $fname , $addr, $cty, $stat, $zip, $acode, $phone");

btw, here's th 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"); <-- you should not put "" on the both sides, this makes $output in only one  array dimention

$output =array($lname, $fname ,  $addr,  $cty,  $stat, $zip, $acode, " - ", $phone); 


ksort($output);
foreach ($output as $directory)
{
    echo "$directory";

    echo " "; //<-- the space you were talking about?
	//You've forgotten to put this
  fwrite($fp, "$director" . " , ");
 
  

}
fclose($fp);
?>
</body>
</html>

If this thing works, Please do mark this thread as Solved.

If not, let me know ;)

Best of wishes

0

Hi Zack,

as you can see, declaring array must be like this

$output =array($lname, $fname , $addr, $cty, $stat, $zip, $acode, $phone);
you should not put "" on between the whole expression. like what you've did:
$output =array("$lname, $fname , $addr, $cty, $stat, $zip, $acode, $phone");

btw, here's th 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");
ksort($output);
foreach ($output as $directory)
{   
    echo "$directory";
    echo " ";
	fwrite($fp, "$directory" . " , ");
}
fclose($fp);
?>
</body>
</html>

It will run in my browser but does not add the dash between the areacode and the phone number as suggested but when I click debug in Codelobster it tells me there is a missing parenthesis and a parse error!!:icon_sad:
Ah well, thanks for the advice either way and happy new year but I cannot flag this as answered yet because your answer does not work!!:icon_neutral:

Edited by zlloyd1: n/a

0

Tell me the sample output(the output you expected to happened) and let me see what I can do.

0

Tell me the sample output(the output you expected to happened) and let me see what I can do.

As I stated initially I need to save directory entries into an array so that I can manipulate them. The output that I am trying to get is the data entered which includes last name, first name, address, city, state, zip code, area code and phone number, to be listed on the screen. The array I am supposed to create is to be a two dimensional array made up of several records as above placed into it as strings and then alphabetized by last name and printed out to the screen. I am not even able to get the array to read to the screen properly and so I cannot really get started on the remainder of the assignment.:icon_cry: I tried the code you posted the first time but it did not produce anything different then what i already had.:icon_frown: What I really need to know is how to echo an array without having to use print_r because I do not want the key numbers printed on the screen but echo does not seem to work with arrays.:icon_sad:

0

Huh? you mean when

echo "$directory";

does not show the arrays??

No that will show the array as long as it is a part of the following code:

foreach ($output as $directory)
{
    echo "$directory";
}

but I need to be able to format the array data before presenting it to the viewer and that is where I am having problems, for instance I need to insert a blank space between the last name and the first name as well as a space between the first name and the address and between the address and the city and the state and another before the zip code and lastly I need to insert a dash between the area code and the phone number and show the array data in that format for all entries to the array from the HTML form. I apologize for being so dense but I am completely new to PHP scripting and this is all alien to me but I am sincerely trying to understand server side programming for my degree and my future employments. In addition to the above mentioned formatting of the presented data I need to alphabetize all entries into the array:icon_eek: and print them out to the screen in alphabetical order. Any assistance would be magical!!:icon_cheesygrin:

This topic has been dead for over six months. 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.