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
Share on other sites

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.

Link to comment
Share on other sites

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.

 

:)

Link to comment
Share on other sites

i'm always drawn to databases for situations like this .. you could have a temporary table in your db that stores the information upon submission, and then the records (information) is easily retrieved and displayed as per the user's request.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.