Set variable according to number record up to number

Asked

Viewed 52 times

2

I have the following idea:

$quantidade = $rowcount;
if $quantidade (?) '(?)') {
$valor = '1';

I need to set the $value by following:

de 100 a 499 = 1
de 500 a 999 = 2
de 1000 a 1499 = 3
de 1500 a 1999 = 4
de 2000 a 3999 = 5
acima de 4000 = quantidade*valor

Can someone help me?

  • 1

    This is essentially: https://answall.com/q/258024/101

  • ta missing one ( ai no if

  • 1

    Assuming this comes from a database, I would already bring calculated from there, via SQL.

3 answers

4

You can use a if and a array, for example:

if($quantidade >= 4000){
    echo $quantidade * 0.03;
} elseif ($quantidade >= 100) {
    echo [1,2,3,4,5,5,5,5][(int)$quantidade/500];
}

If the number is greater than or equal to 4000 will multiply by 0.03. If it is less than 4000 and greater than 100 it will take the value of the array, which is exactly 1 up to 5.

2

Simply use if, which would have the same effect as your switch, only that the code would probably get a little smaller (no need to break of case):

$valor = <valor inicial aqui>;
$quantidade = $rowcount;

if ($quantidade >= 4000) {
    $valor = $quantidade * $valor;
} elseif ($quantidade >= 2000) {
    $valor = 5;
} elseif ($quantidade >= 1500) {
    $valor = 4;
} elseif ($quantidade >= 1000) {
    $valor = 3;
} elseif ($quantidade >= 500) {
    $valor = 2;
} elseif ($quantidade >= 100) {
    $valor = 1;
} else {
    die('O valor inserido é menor que 100');
}

echo $valor;

Look stayed until very readable and simple to understand

1


Thank you Inkeliz.

I Acabai doing so:

switch($quantidade) {
    case ($quantidade <= '100'): {
        $valor = '1';
        break;
    }
    case ($quantidade <= '500'): {
....

 default: {
        $valor = $rowcount*valor;
    }
}
  • 1

    So if it’s less than 100 it must be 1 too?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.