giba Posted January 31, 2009 Share Posted January 31, 2009 Hi, I am running across a situation. I have a script that dinamically creates fields to a list of products. I have 4 fields:PRODUCT, PRICE, QUANTITY and TOTAL The problems is: "How to multiply the product by the quantity for each array?" See below: <input name="price[]" /><input name="quantity[]" /><input name="total[]" /> Considering that I have created 3 items to my list: ??? #1 <input name="price[]" /><input name="quantity[]" /><input name="total[]" /> #2 <input name="price[]" /><input name="quantity[]" /><input name="total[]" /> #3 <input name="price[]" /><input name="quantity[]" /><input name="total[]" /> I don't realize how to calculate it! And the things get worst when thinking about calculating the total amount of totals for a final price <input name="total[]" /> + <input name="total[]" /> + <input name="total[]" /> Hope someone could help me about this mathematical problem, because I am not used to deal with calculations when programming. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/143249-how-to-calculate-the-total-value-of-an-array-of-fields/ Share on other sites More sharing options...
Psycho Posted January 31, 2009 Share Posted January 31, 2009 Here's a very simple example. You would want to add additional validation/error handling and I suspect you would want the total fields to be read only. <html> <head> <script type="text/javascript"> function isNum(value) { return 123; } function calcTotals() { var grandTotal = 0; var row = 0; while (document.forms['cart'].elements['price[]'][row]) { priceObj = document.forms['cart'].elements['price[]'][row]; qtyObj = document.forms['cart'].elements['quantity[]'][row]; totalObj = document.forms['cart'].elements['total[]'][row]; if (isNaN(priceObj.value)) { priceObj = ''; } if (isNaN(qtyObj.value)) { qtyObj = ''; } if (priceObj.value && qtyObj.value) { totalObj.value = (parseFloat(priceObj.value) * parseFloat(qtyObj.value)); grandTotal = grandTotal + parseFloat(totalObj.value); } else { totalObj.value = ''; } row++; } document.getElementById('grand_total').value = grandTotal; return; } </script> </head> <body> <form name="cart"> <table> <tr><th>Price</th><th>Quantity</th><th>Total</th></tr> <tr> <td><input name="price[]" onchange="calcTotals()" /> </td><td><input name="quantity[]" onchange="calcTotals()" /> </td><td><input name="total[]" /></td> </tr><tr> <td><input name="price[]" onchange="calcTotals()" /> </td><td><input name="quantity[]" onchange="calcTotals()" /> </td><td><input name="total[]" /></td> </tr><tr> <td><input name="price[]" onchange="calcTotals()" /> </td><td><input name="quantity[]" onchange="calcTotals()" /> </td><td><input name="total[]" /></td> </tr><tr> <th colspan="2" style="text-align:right;">Grand Total</td> <td><input name="gTotal" id="grand_total" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/143249-how-to-calculate-the-total-value-of-an-array-of-fields/#findComment-751465 Share on other sites More sharing options...
giba Posted January 31, 2009 Author Share Posted January 31, 2009 Perfect! You solved my problem! It is exactly what I need! Thanks and yes, I in fact I will make the total field ready only. Link to comment https://forums.phpfreaks.com/topic/143249-how-to-calculate-the-total-value-of-an-array-of-fields/#findComment-751542 Share on other sites More sharing options...
iSenses Posted April 17, 2013 Share Posted April 17, 2013 i'm very sorry to bump this topic up, but i found this solution because i need it to implement in my webshop. I've got a small problem with it, that's that i need 2 fields before it works. if i have 1 field grand total gives 0 on quantity * price. Can someone give me a solution for this?! Kind regards Kevin Link to comment https://forums.phpfreaks.com/topic/143249-how-to-calculate-the-total-value-of-an-array-of-fields/#findComment-1425424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.