0

I have a project I am near completing except I cannot properly test it because the array uses sessions and is full of useless data now. I have an HTML entry form that feeds data to 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 "Errors Please reenter data<br \>\n";
else{
	 
      session_start();
      if(!isset($_SESSION['Sword']))
     {
      session_register();
      $_SESSION['sword']=array();
     }
     $iterator=count($_SESSION['sword']);
     $_SESSION['sword'][$iterator][] = wordCheck($lname , "lastname");
	 $_SESSION['sword'][$iterator][] = wordCheck($fname , "firstname");
	 $_SESSION['sword'][$iterator][] = wordCheck($addr , "address");
     $_SESSION['Sword'][$iterator][] = wordCheck($cty , "city");
     $_SESSION['Sword'][$iterator][] = wordCheck($stat , "state");
     $_SESSION['Sword'][$iterator][] = wordCheck($zip , "zipcode");
     $_SESSION['Sword'][$iterator][] = wordCheck($acode , "areacode");
     $_SESSION['Sword'][$iterator][] = wordCheck($phone, "telephone");
asort($_SESSION);}
foreach($_SESSION as $directory){
  print_r($directory);
}
?>
</body>
</html>

but unfortunately I am at a loss for how to use the echo command to read the data stored in the array to the screen without including the keys as with the print_r function. I need to read the data out as entries and not array values. If anyone could help me by showing me where and how to place the echo command in order to properly echo the array I would be eternally grateful!!:icon_smile:

Also if possible could you tell me where and how to place the unset() function to clear out the existing array values so that I can continue with testing??:icon_question:

Edited by zlloyd1: one more thing needed

3
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by zlloyd1
0

by this:

foreach($_SESSION as $directory){
for($i=0;$i<count($directory);$i++)
{
	$r_values=$directory[$i];
	foreach($r_values as $value)
	{
		echo $value;echo "<br>";
	}
}
}
0

by this:

foreach($_SESSION as $directory){
for($i=0;$i<count($directory);$i++)
{
	$r_values=$directory[$i];
	foreach($r_values as $value)
	{
		echo $value;echo "<br>";
	}
}
}

I tried your code and I get repeated results for many of the entries being echoed onto the screen.:icon_surprised: I do however appreciate the attempt to help me here!!:icon_wink:

0

every time you refresh the page then the session will keep storing the values you entered. thats why you are getting duplicate values again and again.

Edited by Shanti C: n/a

0

You can remove duplicate values from the array with array_unique(); .

$array = array_unique($array);
0

every time you refresh the page then the session will keep storing the values you entered. thats why you are getting duplicate values again and again.

Yes I am aware of this but it is difficult for me to test the results because the array is so crowded now and I need to clear it out before I can continue!!:icon_sad:
But again, thanks for the code help!!:icon_smile:

0

You can remove duplicate values from the array with array_unique(); .

$array = array_unique($array);

Okay that seems useful but I am not sure where to put that in my code. Would I put it in the sessions part

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($fname , "firstname");
	 $_SESSION['words'][$iterator][] = wordCheck($addr , "address");
     $_SESSION['words'][$iterator][] = wordCheck($cty , "city");
     $_SESSION['words'][$iterator][] = wordCheck($stat , "state");
     $_SESSION['words'][$iterator][] = wordCheck($zip , "zipcode");
     $_SESSION['words'][$iterator][] = wordCheck($acode , "areacode");
     $_SESSION['words'][$iterator][] = wordCheck($phone, "telephone");
//      print_r ($_SESSION['words']);}
$_SESSION = array_unique($_SESSION);

or should I put it in the echo part

foreach($_SESSION as $directory){
for($i=0;$i<count($directory);$i++)
{
$r_values=$directory[$i];
foreach($r_values as $value)
{
echo $value;echo "<br>";
}}}}

??:icon_question:
Could you please show me where I could introduce this code snippet to get the effect please!!

Edited by zlloyd1: not finished

0

The first one should be correct.

What does that mean exactly "the first one should be correct"??
The first what?? And what do you mean should be correct??
Please clarify:icon_question:

0

I meant that

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($fname , "firstname");
	 $_SESSION['words'][$iterator][] = wordCheck($addr , "address");
     $_SESSION['words'][$iterator][] = wordCheck($cty , "city");
     $_SESSION['words'][$iterator][] = wordCheck($stat , "state");
     $_SESSION['words'][$iterator][] = wordCheck($zip , "zipcode");
     $_SESSION['words'][$iterator][] = wordCheck($acode , "areacode");
     $_SESSION['words'][$iterator][] = wordCheck($phone, "telephone");
//      print_r ($_SESSION['words']);}
$_SESSION = array_unique($_SESSION);

should work.

When I said "first one" I meant the first part of code you posted.

0

by this:

foreach($_SESSION as $directory){
for($i=0;$i<count($directory);$i++)
{
	$r_values=$directory[$i];
	foreach($r_values as $value)
	{
		echo $value;echo "<br>";
	}
}
}

While I appreciate you feeding me some code when I use this it only seems to work for the first entry to the directory and if you enter any more they show up as array values in brackets or not at all.:icon_eek:

This is the code I have now including your code snippet:

<!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
 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($fname , "firstname");
	 $_SESSION['words'][$iterator][] = wordCheck($addr , "address");
     $_SESSION['words'][$iterator][] = wordCheck($cty , "city");
     $_SESSION['words'][$iterator][] = wordCheck($stat , "state");
     $_SESSION['words'][$iterator][] = wordCheck($zip , "zipcode");
     $_SESSION['words'][$iterator][] = wordCheck($acode , "areacode");
     $_SESSION['words'][$iterator][] = wordCheck($phone, "telephone");
//print_r ($_SESSION['words']);}
$_SESSION = array_unique($_SESSION);
asort($_SESSION);

foreach($_SESSION as $directory){
for($i=0;$i<count($directory);$i++)
{
$r_values=$directory[$i];
foreach($r_values as $value)
{
echo $value;
echo "<br>";
}}}
?>
</body>
</html>

but as I said this only works for one entry into the form and then seems to glitch out!!:icon_sad: Still I will keep working with your idea and see if I can get it to work somehow. The thing is this array is supposed to collect input from multiple visitors to a web site and store it for manipulation as strings and then needs to be able to read the information on the screen in normal readable form and not in an array form with keys and brackets for each entry and this is just how to echo out one entry but then somehow reverts back to the same output you get from print_r() which is the values in the array in brackets with the keys in brackets as well and that is not the output that is required. Also all of the input strings are to be saved in a single array for ease of access and manipulation in future projects. Still, thanks again for the suggestion and I will keep working on it goodbye!!:icon_smile:
Zack:icon_biggrin:

Edited by zlloyd1: misspelling

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.