How to get sum of few radio button values in a foreach loop

following foreach loop run inside another foreach loop

foreach(array_combine($hotels, $charges) as $hotel => $charge){       
    echo "<input type='radio' name='$location' value='$charge'> $hotel - <b>Rs.$charge.00</b><br>";
}

$charge is some integer value, I want to get the sum value of this radio button selections. How can I do this?

Upvotes: 0

Views: 491

Answers (1)

Rene Korss
Rene Korss

Reputation: 5484

Don't know if you wanted only selected checkboxes sum or all. If all, then this will work.

Create new variable and add $charge to it on every iteration.

$chargeSum = 0;
foreach(array_combine($hotels, $charges) as $hotel => $charge){       
    echo "<input type='radio' name='$location' value='$charge'> $hotel - <b>Rs.$charge.00</b><br>";
    $chargeSum += $charge;
}
echo 'Sum: '.$chargeSum;

Otherwise you have to use jQuery, this question has couple of answers for this.

Upvotes: 1

Related Questions