Jump to content

[SOLVED] array help please


spires

Recommended Posts

Hi guys.

 

I'm completely stuck.

I want to be able to add a new value to an array every time the form is submitted.

 

Example:

Click submit once - $f1Array[0] = $quantity;

Click submit twice - $f1Array[1] = $quantity; 

 

However, I just can't get it to work.

 

Here's my code

$f1Array = array();


if (isset($_POST['submit'])) {

$quantity = $_POST['quantity'];
$product = $_POST['product'];
$itemComp = $_POST['itemComp'];
$unitPrice = $_POST['unitPrice'];
$qtyPrice = $_POST['qtyPrice'];
$gp = $_POST['gp'];



if ($unitPrice=='' || $unitPrice=='0' || $unitPrice=='0.00'){
$finalGP = $itemComp - $product;
$gpTotal = $finalGP * $quantity;
}else{
$finalGP = $itemComp - $product + $unitPrice;
$gpTotal = $finalGP * $quantity;
}

$finalUnit = $quantity * $unitPrice;


if ($f1Array[0] == '')
{
   $f1Array[0] = $quantity;
}elseif ($f1Array[1] == '')
{
   $f1Array[1] = $quantity;
}

}

 

Then, I'm echoing out each array in the HTML body

<?PHP echo $f1Array[0].'<br>'.$f1Array[1].'<br>'.$f1Array[2].'<br>'.$f1Array[3]; 
?>

 

Can some one please advise

Link to comment
https://forums.phpfreaks.com/topic/155222-solved-array-help-please/
Share on other sites

Thats great thanks.

 

But what I want it to do is add a new value into the array.

 

Example:

When I output the results, I want to have each value of the array on a new line.

e.g

f1Array[0]<br>

f1Array[1]<br>

f1Array[2]<br>

f1Array[3]<br>

and so on.

 

Please see http://www.businessmobiles.com/comcalc/main_interface2.php

This is using your code.

try that out .. play around with it a bit...

<?PHP
foreach ($_POST as $k => $v)
{
$asdf[] = $v;
}
print_r($asdf);
?>
<form name="form1" action="" method="post">
<input name="quantity" type="text" size="6" value="45"/>
  
<select name="product" onchange="this.style.backgroundColor = '#FFCCCC'">
	<option value=""> </option>
	<option value="1">add 1</option>
	<option value="2">add 2</option>
	<option value="3">add 3</option>
	<option value="4">add 4</option>
	<option value=""> </option><option value="450">Nokia</option>
</select>
  
<select name="itemComp" onchange="this.style.backgroundColor = '#FFCCCC'">
	<option value=""> </option><option value="340" selected="selected">Orange</option>
</select>
  
<input name="unitPrice" type="text" size="14" value="0.00"/>
  
<input name="qtyPrice" type="text" size="14" value="0"/>
  
<input name="gp" type="text" size="14" value="15300"/>
  
<input type="submit" name="submit" value="add" /> 
</form>
<?php
foreach ($asdf as $value)
{
echo $value.'<br />';
}
?>

 

just a very, very basic way to do this .. better that you just learn what is going on before getting into any more advanced techniques.

Hi

 

Thanks for all your help, I really appreciate it.

 

3.jpg

 

The image above shows the form the i'm trying to create.

The user selects the information from the top section, once 'add' is clicked, all the information will appear at the bottom.

 

If the user then fill out another 'add to current proposal' section, then this will appear below the last lot of results in the 'current proposal handset details' section.

 

I was thinking that this should be done using an array. Each time the form is submitted, it will add the field values into an array, then display them like in the picture.

 

Thanks once again,

Any more help would be great.

 

:)

you need to first decide how/where you're going to store the form's state in between submits. you've at least two choices - the client-side form or the user's server-side session. with something this simple and not knowing a lot about your application, i've done it both ways, but typically lean toward the client-side form. with each form submit build your array from the saved state, then evaluate and perform the action the user selected - add, remove, edit, udpate.

 

jason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.