开发者

how can I modify this script to add instead of incrementing

开发者 https://www.devze.com 2022-12-19 15:26 出处:网络
<?php $aDoor = $_POST[\'formDoor\']; if(empty($aDoor)) { echo(\"You didn\'t select any buildings.\");
<?php
  $aDoor = $_POST['formDoor'];
  if(empty($aDoor)) 
  {
    echo("You didn't select any buildings.");
  } 
  else 
  {
    $N = count($aDoor);

    echo("Y开发者_Go百科ou selected $N door(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($aDoor[$i] . " ");
    }
  }
?>

The script above will increment and then echo the extracted values of the variable array $aDoor, How can I modify to sum the values instead?


$total = 0
for($i=0; $i < $N; $i++)
{
  $total += $aDoor[$i];
}
echo "Total: $total";


$sum = 0;
foreach($aDoor as $value){
    $sum += $value;
}
echo 'Sum: ' . $sum;


You mean sum the first i elements of the $aDoor array?

something like this maybe:

$sum = 0;
for ( $i = 0; $i < $N; $i++ )
{
  $sum += $aDoor[$i];
}


<?php
  $aDoor = $_POST['formDoor'];
  if(empty($aDoor)) 
  {
    echo("You didn't select any buildings.");
  } 
  else 
  {
    $N = count($aDoor);

    //echo("You selected $N door(s): ");
    var $iDoors = 0;
    for($i=0; $i < $N; $i++)
    {
      //echo($aDoor[$i] . " ");
      $iDoors += $aDoor[$i];
    }
    echo("You selected $iDoors door(s): ");
  }
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消