# Help: Posting Multiple Arrays And Calculating Their Values.

## Recommended Posts

This one is a doozy for me. My brain is fried after spending a couple days trying to figure this out. None of my formulas have seemed to work out enough. So I'm waiving my white flag and asking for some help.

Below is a simplified version of my code. The purpose of this page is an estimate calculator. It is for a window cleaning service, and they have pricing for doing the insides of windows and the outsides. Each style of window has different pricing for each inside and outside.

What is supposed to happen in the form is this: If they want an estimate for say basic window cleaning for both inside and outside, they will check the chechboxes for both inside and outside. Then enter a number for the amount of windows to clean. The formula for this would look something like this: Amount = Inside Price x Outside Price (if inside price and outside price checkboxes are ticked). And for each entry, it needs to add up the total. But none of the foreach or while loops i try to create seem to do the trick.

Basic Form Setup:

```
\$query = "SELECT * FROM estimates WHERE 1 ORDER BY id ASC";
\$result = mysql_query(\$query);

while (\$estimates = mysql_fetch_object(\$result)) {

\$style = \$estimates->style;
\$insidePrice = \$estimates->price;
\$outsidePrice = \$estimates->out;

echo \$style.'
<input type="checkbox" name="insidePrice[]" value ="'.\$insidePrice,'" /> \$'.\$insidePrice.'
<input type="checkbox" name="outsidePrice[]" value ="'.\$outsidePrice,'" /> \$'.\$outsidePrice.'
<input type="text" name="amount[]" />';
}
```

And an idea of how i want this to work:

```
foreach (\$_POST['amount'] as \$key => \$a){
if (\$a != "" or \$a !="0"){
//Now to llustrate what Im trying to do, I know this is wrong though
foreach (\$_POST['insideAmount'] as \$i){
\$i = \$i*\$a;
}
foreach (\$_POST['outsideAmount'] as \$o){
\$o = \$o*\$a;
}
\$a = \$i+\$o;
}
//Trying to add each row as they build to make the estimate total
\$prev = \$a[\$key-1];
\$a = \$a+\$prev;
\$total = \$a;
}

echo "\$total";
```

##### Share on other sites

Also, when I was able to at least return data from each checkbox, it wasn't returning the value. It would only display "on". Couldn't figure out why.

##### Share on other sites

```\$total = 0;
foreach (\$_POST['amount'] as \$key => \$a) {
\$i = \$_POST['insideAmount'][\$key];
\$o = \$_POST['outsideAmount'][\$key];
\$total += \$a * (\$i + \$o);
}
echo \$total;
```

##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.