two array in Foreach
Good morning, folks.
I have a question here.
I have three select. Brings first product, quantity brings Second, Third brings value. These are more select muntiplos as an array;
Ex:
$quantity = "1,2,3";
$value = "20.00,30.00,20.00";
Foreach had to do it.
qty value
$S ="1 * 20.00";
$S ="2 * 30.00";
$S ="1 * 20.00";
I am sending the page to have two to do the calculation. More not getting to do two foreach with the data value and quantity.
Anyone know how I can sum this Vazer?
I would be very grateful.
EX:
<select class="product" name=product[]" id="product[]">
<option value="1"> Plan 1 </ option>
<option value="2"> Plan 2 </ option>
</ select>
<select class="quantity" name=quantity[]" id="quantity[]">
<option value="1"> 1 </ option>
开发者_开发技巧 <option value="2"> 2 </ option>
</ select>
<select class="value" name=value[]" id="value[]">
<option value="1"> 20:00 </ option>
<option value="2"> 30.00 </ option>
</ select>
Post
$quantity = $_POST["quantity"];
$value = $_POST["value"];
$quantity = explode(',', $quantity_e);
$value = explode(',', $value_e);
foreach ($quantity as $q && $value as $v) { // ???
$v = $v * $q; // ????
}
Well, first of all, you're not going to get any relevant information from your selects. Your 3 input arrays will look like this
Array
(
[name] => (
[0] => 1
[1] => 2
[2] => 1
)
[quantity] => (
[0] => 1
[1] => 1
[2] => 2
)
[value] => (
[0] => 2
[1] => 2
[2] => 2
)
)
You are not passing anythign but 1's and 2's because the value
attribute of all of your options is set to only 1's and 2's.
You can either remove the value
attribute all together and it will pass the text in options, or you could set value
to the actual value you want to pass.
Once you have the data in to format you originally specified, something like this would accomplish what I believe to be your goal.
// make arrays from your comma delimited strings
$quantity_array = explod(',',$quantity);
$value_array = explode(',',$quanity);
// variable to store the total
$total = 0;
//iterate over them, summing them up
foreach ( $quantity_array as $i => $qty ) // $i is the index
{
$item_total = $qty * $value_array[$i]; // the index should be the same for the values
$total += $item_total;
}
精彩评论