Calculate as much as the value increases with a fixed value [closed]

I am trying to calculate the total amount for each person with the following for statement.

The default is 3 people and the cost is 50 dollars, By default, we want to add $10 for each additional person.

For example, When the number of people stored is 6, 6 people * 50 dollars = not 300 dollars 3 people (default $50) + 3 people (additional $30) = $80.

I’m trying to get it as above.

for ($i=0; $row=sql_fetch_array($result); $i++) {
  $standard = '50';

  if ($row['person'] > 3) {
    echo $person_calculate = $row['person'] * $standard;
  }
  else
  {
    echo '0';
  }

}

Answer

Just need to use basic algebra…

Given the following values:

$breakPoint = 3;
$basis = 50;
$secondary = 10;
$population = $row['person'];

$total = $breakPoint * $basis + ($population - $breakPoint) * $secondary;

Now, just stick it in a loop:

<?php
// get db connection info
// ...

$stmt = $pdo->query('your query here');

$breakPoint = 3;
$basis = 50;
$secondary = 10;

// loop through db results
foreach($stmt as $row) {
    // set default value if 3 or fewer
    $total = $breakPoint * $basis;

    /: calculate for over 3 
    if($row['person'] > 3) {
        $total = $breakPoint * $basis + ($population - $breakPoint) * $secondary;
    }
    print $total . '<br>';
}

Important note

You are working with a deprecated version of php! You need to upgrade to take advantage of PDO or mysqli.

See this site for info on how to use PDO

Leave a Reply

Your email address will not be published. Required fields are marked *