phpstuck Posted January 31, 2010 Share Posted January 31, 2010 Ok, I am using JavaScript for a form, it generates a new row for data entry should the user need to add more parts and pricing, when I post this to the PHP handler I would like to get a total of the Array for $cost only... So far I have managed to create a loop that has no exit and I am burned out. I have researched this using Google and managed to get a virus from another Forum that took two entire days to root out. I hope someone here can help me solve this First the Form. <HTML> <HEAD> <TITLE> Add/Remove dynamic rows in HTML table </TITLE> <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; //alert(newcell.childNodes); switch(newcell.childNodes[0].type) { case "text": newcell.childNodes[0].value = ""; break; case "checkbox": newcell.childNodes[0].checked = false; break; case "select-one": newcell.childNodes[0].selectedIndex = 0; break; } } } function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowCount <= 1) { alert("Cannot delete all the rows."); break; } table.deleteRow(i); rowCount--; i--; } } }catch(e) { alert(e); } } </SCRIPT> </HEAD> <BODY><FORM action=testjs.php method=post> <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" /> <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" /> <TABLE id="dataTable" width="350px" border="1"> <TR> <TD><INPUT type="text" name="partnum[]"/></TD> <TD><INPUT type+"text" name="desc[]"/></TD> <TD><INPUT type="text" name="cost[]"/></TD><br> <TABLE id="dataTable1" width="350px" border="1"> <TR> <TD><INPUT type=submit value="Generate estimate now" name=B1></B></P></TD> </TR> </TABLE> </FORM> </TR> </TABLE> </BODY> </HTML> Then the handler that needs to get the sum of the $cost Array: <?php $partnum = $_POST['partnum']; $desc = $_POST['desc']; $cost = $_POST['cost']; foreach($desc as $a => $b) echo "$partnum[$a] - $desc[$a] - $cost[$a] <br />"; for ($i=0; $i<$cost; $i++) { echo $_POST['cost'][$i]."<br>"; } $test = array($_POST['cost']); echo "Sum of the array is: ".array_sum($test)."<br>"; ?> Any help figuring this out would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/ Share on other sites More sharing options...
RussellReal Posted January 31, 2010 Share Posted January 31, 2010 $test = (array) $_POST['cost']; $total = array_sum($test); Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/#findComment-1004688 Share on other sites More sharing options...
phpstuck Posted January 31, 2010 Author Share Posted January 31, 2010 Oh yeah.... there it is... I know it goes without saying, but Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/#findComment-1004690 Share on other sites More sharing options...
phpstuck Posted January 31, 2010 Author Share Posted January 31, 2010 Still not working right it prints: jh2345 - Brakes - 2 jh1534 - rotors - 2 jh6721 - fluid - 2 2 2 2 and then never fully loads the page as if it is still thinking Modified Code: <?php $partnum = $_POST['partnum']; $desc = $_POST['desc']; $cost = $_POST['cost']; foreach($desc as $a => $b) echo "$partnum[$a] - $desc[$a] - $cost[$a] <br />"; for ($i=0; $i<$cost; $i++) { echo $_POST['cost'][$i]."<br>"; } $test = (array) $_POST['cost']; $total = array_sum($test); echo "Sum of the array is: ".array_sum($total)."<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/#findComment-1004744 Share on other sites More sharing options...
RussellReal Posted February 1, 2010 Share Posted February 1, 2010 you're nesting your code wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/#findComment-1004831 Share on other sites More sharing options...
phpstuck Posted February 1, 2010 Author Share Posted February 1, 2010 Thanks! I now have it working like a dream, sometimes you just get all boneheaded on average stuff. I was trying to make the picture bigger than it was. I appreciate your help.... The code that works for anyone else who might wander into this: <?php $partnum = $_POST['partnum']; $desc = $_POST['desc']; $cost = $_POST['cost']; foreach($desc as $a => $b) echo "$partnum[$a] - $desc[$a] -".'$'." $cost[$a] <br />"; $test = (array)$_POST['cost']; $total = array_sum($test); echo '<br><hr>'."Parts total for job 1: ".'<b>'."$".number_format($total,2)."<br></b>"; $subttl = (($total)*(".06")); echo '<br><b>'."Tax: $".'</b>'.number_format($subttl,2); $ttl = (($total)+($subttl)); echo '<br><b>'."Total Estimate: $".'</b>'.number_format($ttl,2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/190465-getting-array-feild-sum-total-using-only-php-no-mysql/#findComment-1004834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.