Hi all,

I have few php pages. One is a dynamic form, one is an file holding some arrays, one is a script that is the action for the form and the other two are to update the array. What I am trying to do is append an array using array_push to add files to the array. Howver it only seems to work local to the page you are on. I am using the array as an include at the moment, however I am assuming sessions are the way round it, but I dont really get it.

Ideally what I want to do is update the array so it can be used over many pages at one time.
Here are the forms
optionsArray.php

$model= array(1=>"Compact",2=>"Saloon",3=>"SUV" );
	$color= array(1=>"Deep Sea Blue", 2 =>"Space Grey", 3=>"Titanium Silver");
	$engine= array( 1=>"1600", 2=> "2000", 3=>"3000");
	$extras= array
		(1=> "6 disc DVD changer",
		 2=>"TV function",
		 3=>"DAB digital radio",
		 4=>"Loudspeaker system - professional", 
		 5=>"Multimedia navigation system",
		 6=>"Universal remote control",
		 7=>"Voice control",
		 8=>"Navigation system",
		 9=>"Head-up display",
		 10=>"Adaptive headlights",
		 11=>"Night vision with pedestrian recognition",
		 12=>"Run-flat tyres");
	$num= array(1=>'one',2=>'two',3=>'three');

the form

<?php include('optionsArray.php');?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cars4U</title>
<link href="./css/blue-1col.css" rel="stylesheet" type="text/css" title="1 Column Style" media="screen"/>
</head>
<body>
<div id="container">
<div id="header"><img src="./images/logo.gif" width="211" height="67" alt="Cars4U Logo" /><br /></div>
<div id="menu">
<ul>
<li><span class="menu"><a href="index.html">home</a></span></li>
<li><span class="menu"><a href="news.html">news</a></span></li>
<li><span class="menu"><a href="order.html">order form</a></span></li>
<li><span class="menu"><a href="gallery.html">gallery</a></span></li>
<li><span class="menu"><a href="services.html">services</a></span></li>
<li><span class="menu"><a href="about.html">about us</a></span></li>
<li><span class="menu"><a href="contact.html">contact us</a></span></li>
</ul>
<p>&nbsp;</p>
</div>
<div id="content">
<h1>Order Form</h1><br />
<form method="post" action="order1.php">
<fieldset>
<legend>Contact details</legend>
<div class="form-field">
<label for="firstname">First name</label>
<input type="text" name="firstname" id="firstname" size="21" maxlength="20" /><br />
<label for="lastname">Last name</label>
<input type="text" name="lastname" id="lastname" size="21" maxlength="20" /><br />
<label for="email">Email address</label>
<input type="text" name="emailaddress" id="email" size="31" maxlength="30" /><br />
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone" size="21" maxlength="20" /><br />
</div>
</fieldset>
<br />
<fieldset>


<legend>Car type</legend>
<?php 
	for($i=0; $i <=count($model);$i++)
	{
	if(!$i==0)
		{
		
		echo '<input name="cartype" type="radio" value="'.($i). '" id="car'.$num[$i].'" />';
		echo '<label for="car'.$num[$i].'">'.$model[$i].'</label><br />';

		}
	}
?>
</fieldset>
<br />
<fieldset>
<legend>Colour</legend>
<?php 
	for($i=0; $i <=count($model);$i++)
	{
	if(!$i==0)
		{
		
		echo '<input name="carcolour" type="radio" value="'.($i). '" id="col'.$num[$i].'" />';
		echo '<label for="col'.$num[$i].'">'.$color[$i].'</label><br />';

		}
	}
?>
</fieldset>
<br />
<fieldset>
<legend>Engine size</legend>
<?php 
	for($i=0; $i <=count($engine);$i++)
	{
	if(!$i==0)
		{
		
		echo '<input name="carenginesize" type="radio" value="'.($i). '" id="eng'.$num[$i].'" />';
		echo '<label for="eng'.$num[$i].'">'.$engine[$i].' cc</label><br />';

		}
	}
?>
</fieldset>
<br />

<fieldset>




<select class="select" id="extras" name="options[]" multiple="multiple" size="6">
<?php
echo'<legend>Optional extras</legend>';
echo'<optgroup label="Entertainment">';
	for($i=0; $i <=count($extras);$i++)
	{	
		if($i <6 && $i>0)
			{
			
			 echo "<option value =\"".$i."\">".$extras[$i]."</option>";
			}
	 }
echo '</optgroup>';
echo '<optgroup label ="Communication">';
	 for($i=0; $i <=count($extras);$i++)
	{	
		if($i >=6 && $i<10)
			{
			
			echo "<option value =\"".$i."\">".$extras[$i]."</option>";
			}
	 }
echo '</optgroup>';
echo '<optgroup label="Safety and Security">';
	for($i=0; $i <=count($extras);$i++)
	{	
		if($i >=10 && $i<13)
			{
			
			 echo "<option value =\"".$i."\">".$extras[$i]."</option>";
			}
	 }

echo '</optgroup>';
 ?>
</select>
</fieldset>
<br />
<fieldset>
<legend>Terms and conditions</legend>
<p class="title">Agree to site terms and conditions:&nbsp;
<input type="checkbox" name="terms" value="yes" /></p>
</fieldset>
<br />
<input class="button" type="submit" value="Submit Form" /> <input class="button" type="reset" value="Clear Form" />
</form>
</div>
<div id="footer">&copy; 2011 Cars4U Ltd. All Rights Reserved.</div>
</div><!-- end of container -->
</body>
</html>

the script for the form

<?php

$terms=$_POST['terms'];
$fname = trim($_POST['firstname']);
$fname = ucfirst($fname);
$lname = trim($_POST['lastname']);
$lname = strtoupper($lname);
$email = trim($_POST['emailaddress']);
$email = strtolower($email);
$phone=trim($_POST['phone']);
$cartype= $_POST['cartype'];
$carcolour = $_POST['carcolour'];
$carenginesize=$_POST['carenginesize'];
$options = $_POST['options'];
include 'optionsArray.php';
		 
		 //Array to store validation errors
	

			echo"<html>
						 <head>
						 <title>Car Order Confirmation</title>
						 <link href=\"./css/blue-1col.css\" rel=\"stylesheet\" type=\"text/css\" title=\"1 Column Style\" media=\"screen\"/>
						 </head>
						 <body>
						 <div id=\"form-result\">
						 ";
	

			
			
			//Validation error flag
			if($terms == ''){
				echo	  '<li class="err">You must agree to our terms and conditions';
				echo '<br/>Please click back</li>';
				
			}
			
			elseif($fname == '') {
				echo '<li class="err">Error: First name missing</li>';
			}
			elseif($lname == '') {
				echo '<li class="err">Error: Last name missing</li>';
				
			}
			elseif($email == '') {
				echo '<li class="err">Error: Email Address missing</li>';
			}
			elseif($phone == '') {
				echo'<li class="err">Error: Phone Number missing</li>';
			
			}
			elseif($cartype == '') {
				echo '<li class="err">Error: Please choose your car type</li>';
				
			}
			elseif($carcolour == '') {
				echo  '<li class="err">Error: Please choose a colour</li>';
			
			}
			elseif($carenginesize == '') {
				echo '<li class="err">Error: Please choose an enginge size</li>';
				
			}

			else{
					echo"<html>
						 <head>
						 <title>Cars4u.com</title>
						 <link href=\"./css/blue-1col.css\" rel=\"stylesheet\" type=\"text/css\" title=\"1 Column Style\" media=\"screen\"/>
						 </head>
						 <body>
						 <div id=\"form-result\">
						 ";
						 
						
						


					echo"<html>
						 <head>
						 <title>Cars4u.com</title>
						 <link href=\"./css/blue-1col.css\" rel=\"stylesheet\" type=\"text/css\" title=\"1 Column Style\" media=\"screen\"/>
						 </head>
						 <body>
						 <div id=\"form-result\">
						 ";
					echo '<p>Dear <span>'.$fname.' '.$lname.'</span>,<br/>
					Thank you for your order</p>';
					echo 'Your contact details are:-<br/> Phone: <span>'.$phone.'</span><br />Email: <span>'.$email.'</span>';
					echo '<p>You have selected the following car:</p>';


					echo 'Car Type: <span>'.$model[$cartype].'</span>';
					echo '<br/>Colour: <span>'.$color[$carcolour].'</span>';
					echo '<br/>Engine Size: <span>'.$engine[$carenginesize].'</span>';
					if($options >0)
					{
					echo '<p>Your extras include: <br/>';

					for($i=0;$i<count($options);$i++)
					{


					$val=$options[$i] ;

					echo '<span>'.$extras[$val].'</span><br/>';
					}
					echo"</p>";
					}
					echo "</div></body>";
				}
	





?>

form to append array

<html>
<head>
<title>Add new Items</title>
</head>
<body>
	<form action="ammendArray.php" method="post">
	<p>Add new items</p>
	
	Add up to 3 new cars <br/>
	Type 1<input type="text" name="CarOne" /><br/>
	Type 2<input type="text" name="CarTwo" /><br/>
	Type 3<input type="text" name="CarThree" /><br/>
        </form>
</body>
</html>

script to change array

<?php
include 'optionsArray.php';
$carone = $_POST['carone'];
$carChange =$_POST['change'];
$shoe;

echo $carone;

for($i=0; $i <=count($carChange); $i++)

{
$shoe=$carChange[$i];
echo $shoe;

$green_key = array_search($shoe, $model); // returns the first key whose value is 'green'
$model[$green_key] = $carone; // replace 'green' with 'apple'
}


print_r($model);
?>

Please help me if you can, and if you want more information please ask

Liam

As you seem to already recognize, when you include the module with the arrays, you are getting a fresh copy every time. If your intent is to pass these arrays around and update them, then you are right again, using session variables would be the best way to do it. In your include module, you could enclose all the definitions in an if:

session_start();
if ($_SESSION['array_flag'] <> "set") {
   $_SESSION['array_flag'] = "set";
   $_SESSION['model'] = array(1=>"Compact",2=>"Saloon",3=>"SUV" );
   ...
}

That way you'll only do the definitions once. All of your arrays need to become Session variables as I showed above for mode1. Anywhere that you use the arrays, you will now reference the Session versions. Any modules that use the session variables must have a session_start statement.

commented: Was simple and easy to understand +1

Thank you very much. It works just how I needed it too.

Thanks again
Liam

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.