How do you turn the average to red?

Asked

Viewed 636 times

5

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

Follows 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>";
  }
?>

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 the CSS:

.notaVermelha{
    color: "#F00";
}
  • I think it would be better to add the span element only in case the note is really red, and not just change the attribute (unless it is an attribute for blue and one for red).

  • @Bacco, thank you for Suggestão. I prefer it this way, to be consistent. So all values have the same "wrapper" and if it is necessary to select them with javascript it is easier to use the span to reach them.

4

Just test if the average is less than 20 before you put it 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>";
}
  • Note that we are missing ' not to break the code around the style.

  • 1

    Ops, corrected!

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;
  • Friend Thanks for the help, thanks really! Very cool draft I will use this reasoning!

2

Just use the 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>";
    }
}
?>

  • 3

    Remembering that the use of the tag <font> is not recommended as it is deprecated from HTML4.01 and HTML5 no longer supports this tag. The ideal is to use CSS.

2

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

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

I hope I’ve helped!

  • Friend helped yes man, I will use another method but your mode just for result.

1

I used the short 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>";
}

Browser other questions tagged

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