Calculate value in PHP

2

I have this code with which I need to upload an image of a specific color according to the value obtained from the database.

Example: > = 11 green img, > = 4 &

asked by Lucho Alberto 22.08.2016 в 23:49
source

3 answers

2

The problem may be the way you are putting $$row_f1['prom'] , if you realize these by putting a plus price sign.

I advise you to avoid these problems, assign them to a variable before opening the conditional. Example:

$rowf1 = $row_f1['prom'];

if ($rowf1 >= 5){
 echo 'Es mayor o igual a 5';
} else {
 echo 'No es mayor o igual a 5';
}

So you avoid having to write something long and tedious as $row_f1['prom'] every time, and it gives more clarity and readability to your code:)

    
answered by 23.08.2016 в 00:39
2

My two cents here are to optimize a bit the code displayed, the error should be the one explained by Hoose (I will base myself slightly on his answer) and A. Felipe Trujillo:

<?php

// almacenamos el valor a evaluar en otra variable
$valor = intval($row_f1['prom']);

// usamos el último valor posible como valor por defecto
// para obviar un condicional
$salida = 3;

if ($valor >= 11) {

    $salida = 1;

} elseif ($valor >= 4 && $valor < 11) {

    $salida = 2;

}

// agregamos el valor resultado a la parte de texto resultante que no cambia
echo "<img src='img/btn$salida.png'>";
    
answered by 23.08.2016 в 01:05
0

Because the function intval takes the whole part of a number, I do not see the sense that in your code you have intval(($row_f1['prom'])) <= '10.9' only with placing intval(($row_f1['prom'])) < 11 is enough

    
answered by 23.08.2016 в 01:09