Jump to content

add and multiply results of a form


skartdo

Recommended Posts

Group the c/boxes on the form into two arrays. When posted then you can use array_sum() on each group

 

The first group all have same name, say, name='gp1[]' and second set have name='gp2[]'.

 

to process

 

$tot1 = array_sum($_POST['gp1'];
$tot2 = array_sum($_POST['gp2'];

 

edit by ober to add closing php tags.

 

  • 3 weeks later...

HTML FORM

<form action="" method="post">
1<br>
<input type="checkbox" value="3" name="cb1[]"><br>
<input type="checkbox" value="6" name="cb1[]"><br>
<input type="checkbox" value="8" name="cb1[]"><br><br>
2<br>
<input type="checkbox" value="1" name="cb2[]"><br>
<input type="checkbox" value="2" name="cb2[]"><br>
<input type="checkbox" value="3" name="cb2[]"><br>
<input type="submit" value="calculate">
</form>

 

php

$group1 = $_POST['cb1'];
$group2 = $_POST['cb2'];
//calculate group 1
$group1_value = 0;
for($i=0; $i<count($group1); $i++){
  $group1_value = ($group1[$i])+($group1_value);
}

//calculate group 2
$group2_value = 0;
for($q=0; $q<count($group2); $q++){
  $group2_value = ($group2[$q])+($group2_value);
}

//multiply group 1 and 2
$end_value = $group1_value*$group2_value;

echo "End Value = ".$end_value;

 

How bout something like that?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.