为了账号安全,请及时绑定邮箱和手机立即绑定

如何添加或倍增 chechkbox 的值

如何添加或倍增 chechkbox 的值

PHP
HUWWW 2021-09-18 16:12:41
我正在尝试使用 foreach 循环总结复选框值的值......但我对在哪里添加符号感到困惑,以便这些值可以相加。我在哪里包括加号,以便它可以总结任何数值,以便我可以得到总数?<?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>
查看完整描述

2 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

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

}


?>


查看完整回答
反对 回复 2021-09-18
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

添加循环中的所有值。


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;


查看完整回答
反对 回复 2021-09-18
  • 2 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信