Jump to content

Getting Array Feild Sum Total (using only PHP no MySQL)


phpstuck

Recommended Posts

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!

Link to comment
Share on other sites

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>";



?>

Link to comment
Share on other sites

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);

?>

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.