How to leave the average in red?

5

How do I make the average result turn red if its value is less than 20?

Follow the code:

<?php
  function media($p1,$p2,$p3) {
    $resultado = ($p1 + $p2 + $p3)/3;
    return $resultado;
  }

  $aluno[0]["nome"] = "Ryan";
  $aluno[0]["media"] = media(10,20,30);

  $aluno[1]["nome"] = "Mikhaela";
  $aluno[1]["media"] = media(40,50,60);

  for($i=0;$i<count($aluno);$i++){
    echo "<b> Nome do Aluno </b>".$aluno[$i]["nome"]."<br>";
    echo "<b> A media do Aluno e </b>".$aluno[$i]["media"]."<br><br>";
  }
?>
    
asked by anonymous 30.01.2014 / 21:38

6 answers

7

I would add a CSS class that would be introduced in php.

For example:

for($i=0;$i<count($aluno);$i++){
    $media = $aluno[$i]["media"];
    $notaVermelha = '';
    if($media < 20){$notaVermelha = "notaVermelha"}

    echo "<b> Nome do Aluno </b>" .$aluno[$i]["nome"]."<br>";
    echo "<b> A media do Aluno e </b><span class='".$notaVermelha."'>".$media."</span>";
}

And in CSS:

.notaVermelha{
    color: "#F00";
}
    
30.01.2014 / 21:49
4

Just test if the average is less than 20 before on the screen.

if($aluno[$i]["media"] < 20){
    echo "<span style=\"color:red\"><b> A media do Aluno e </b></span>" .$aluno[$i]["media"]."<br><br>";
}else{
    echo "<b> A media do Aluno e </b>" .$aluno[$i]["media"]."<br><br>";
}
    
30.01.2014 / 21:49
3

Just add an if inside the for

$resultado = '';
for($i=0;$i<count($aluno);$i++){
    $cor = 'black';
    if($aluno[$i]['media'] <= 20 ){
        $cor = 'red';
    }
    $resultado .=  "<p style='color:$cor;'><b> A media do Aluno e </b>"
    .$aluno[$i]["media"]."<br></P>";
}

echo $resultado;
    
30.01.2014 / 21:45
2

You can assign a class in "b" and by formatting css with the desired color. In this case the whole line will turn red. If you want only the result of the average in red can do that.

echo "<b> A media do Aluno e <span style='color: red;'>".$aluno[$i]["media]."</span><b><br>";

I hope I have helped!

    
30.01.2014 / 21:46
2

Just use if .     

$aluno[0]["nome"] = "Ryan";
$aluno[0]["media"] = media(10,20,30);

$aluno[1]["nome"] = "Mikhaela";
$aluno[1]["media"] = media(40,50,60);


for($i=0;$i<count($aluno);$i++) {
    echo "<b> Nome do Aluno </b>" .$aluno[$i]["nome"]."<br>";
    if($aluno[$i]["media"] < 20) {
        echo "<font color=\"red\"><b> A media do Aluno e </b>" .$aluno[$i]["media"]."</font><br><br>";
    } else {
        echo "<b> A media do Aluno e </b>" .$aluno[$i]["media"]."<br><br>";
    }
}
?>
    
30.01.2014 / 21:43
1

I used the summary form of if to make the code drier.

for($i=0; $i < count($alunos); $i++){
    $alunos[$i]["media"] <= 5?$cor='red':$cor='blue';
    echo "<b>Nome do Aluno: </b>".$alunos[$i]["nome"]."<br>";
    echo "<p style='color:$cor;'><b>Media final: </b>".$alunos[$i]["media"]."<br><br></p>";
}
    
06.05.2015 / 18:17