Get values from HTML in a multidimensional array and calculate values using PHP

Posted by Frank Nwoko on Stack Overflow See other posts from Stack Overflow or by Frank Nwoko
Published on 2012-05-24T21:46:15Z Indexed on 2012/06/02 4:40 UTC
Read the original article Hit count: 147

Filed under:
|

I have searched but could not get solution on this issue.

I am working on an application which will generate unknown number of items and have users select the quantity from a drop down against each item. Eg.

Item(s) | price | Select qty
Rice     23      3
Beans    22      4
Eggs     52      5

...
...
...
unknown

Please, how can I capture the above in an array and also calculate the total value for all selected items and corresponding fees?

I have the following HTML code:

<form id='form1' name='form1' method='post' action='item_calc.php'>
<?php
    .....
while($t_row = mysql_fetch_assoc($get_items)) {

echo "<p><label>$t_row['$item_name']
<input type='text' READONLY name='item_price[]' value='$t_row['$item_price']' /></label>
<input type='text' READONLY name='item_fees[]' value='$t_row['$item_fee']' /> 
<select name="item_qty">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
<option value="5">
</select>
</p><p>";

}

        echo "<label><input type='submit' name='submit'  value='Submit' /></label></p>
         </form>";

Please, how can I get item_price[] + item_fees[] * item_qty for all selected items?

This is what I have tried:

for ($i = 0; $i < count($_POST['item_price']); $i++) {
   // Do something here with $_POST['checkbx'][$i]



   foreach ($_POST['item_fees'] as $tkey => $tvalue) {
    //echo "Key: $tkey; Value: $tvalue<br>";
   }


   foreach ($_POST['item_price'] as $pkey => $pvalue) {
    //echo "Key: $pkey; Value: $pvalue<br>";
   }

   $total = $tvalue + $pvalue;


}
echo $total;

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays