I am trying to sum up the values of checkbox value using a foreach loop... but i am confused as to where to add the signs so the values can add up.
Where do i include the addition sign so it can sum up whatever number value so i can get the total?
<?php
if(isset($_POST["submit"])){
$main_odds = $_POST["total_odds"];
if(!empty($main_odds)){
echo "You have selected the following games ";
foreach ($main_odds as $final_odd){
echo "<td>$final_odd</td>" ;
}
}else{
echo "You have not selected any odds";
}
}
$total_odds = "";
?>
<table>
<thead>
<tr>
<th>calculate</th>
<th>odds</th>
</tr>
</thead>
<tbody>
<?php foreach($games as $game): ?>
<tr>
<td><?= $game->odds; ?></td>
<td>
<form method="Post" action="">
<input type="checkbox" name="total_odds[]" value="<?= $game->odds; ?>">
</td>
</tr>
<?php endforeach; ?>
<input type="submit" name="submit"/>
</form>
</tbody>
</table>
Add all values within the loop.
echo "You have selected the following games: ";
$total = 0;
foreach ($main_odds as $final_odd){
$total += $final_odd;
echo "<td>$final_odd</td>" ;
}
echo "<br>Total value is " . $total;
<?php
$main_oddddd = "";
if(isset($_POST["submit"])){
$main_odds = $_POST["total_odds"];
if(!empty($main_odds)){
$val = 0;
echo "You have selected the following games ";
foreach ($main_odds as $final_odd){
$val += $final_odd;
}
}else{
echo "You have not selected any odds";
}
$main_oddddd = count($main_odds);
echo $val;
}
?>
<?php
$main_oddddd = "";
if(isset($_POST["submit"])){
$main_odds = $_POST["total_odds"];
//echo $main_value = implode(", ", $main_odds);
$total = 0;
if(!empty($main_odds)){
echo "You have selected the following games ";
foreach ($main_odds as $final_odd){
$total = $total * $final_odd;
}
}else{
echo "You have not selected any odds";
}
$main_oddddd = count($main_odds);
echo $total;
}
?>