Displaying data in a table with foreach

1

I'm having problems on the results page, others are working correctly, the purpose is to print the approval or disapproval of the students in the table using foreach in PHP.

Error:

  

Warning: Invalid argument supplied for foreach () in

<?php            

$nota1 = $_POST["matematica"];
$nota2 = $_POST["portugues"];
$nota3 = $_POST["quimica"];
$nota4 = $_POST["fisica"];
$nota5 = $_POST["geografia"];
$nota6 = $_POST["historia"];

$notas = [$nota1, $nota2, $nota3, $nota4, $nota5, $nota6];

function calculaAprovacao($nota){
    foreach($nota as $lista){
    if($lista >= 60){

        return "Aprovado";
    }else{

        return "Reprovado";
    }
    }
}


?>

<?php include "cabecalho.php"?>


<table border="1px">
    <?php foreach(calculaAprovacao($notas) as $aprovacao): ?>
    <tr>
        <th>Matemática</th>
        <td><?php echo $aprovacao[0]; ?></td>
    </tr>
    <tr>
        <th>Português</th>
        <td><?php echo $aprovacao[1]; ?></td>
    </tr>
    <tr> 
        <th>Química</th>
        <td><?php echo $aprovacao[2]; ?></td>
    </tr>

     <tr>
            <th>Física</th>
            <td><?php echo $aprovacao[3]; ?></td>
        <tr>

        <tr>
            <th>Geografia</th>
            <td><?php echo $aprovacao[4]; ?></td>
        <tr>

    <tr>
        <th>História</th>
        <td><?php echo $aprovacao[5]; ?></td>
    </tr>
        <?php endforeach; ?>
</table>


</body>
</html>
    
asked by anonymous 06.03.2015 / 19:13

3 answers

4

The use of foreach does not seem to make sense there. in HTML you are repeating all 6 lines ( <tr> ) so you do not have to use foreach . And then you do not have to use foreach inside the function that takes the approval text, since it is executing everything individually (in fact I think this function should not have foreach same). In the current form any use of loop is an unnecessary complication. If you want to use a loop then make an appropriate code. I'll show you both ways.

Following your algorithm without loop :

<?php            
$nota1 = $_POST["matematica"];
$nota2 = $_POST["portugues"];
$nota3 = $_POST["quimica"];
$nota4 = $_POST["fisica"];
$nota5 = $_POST["geografia"];
$nota6 = $_POST["historia"];

$notas = [$nota1, $nota2, $nota3, $nota4, $nota5, $nota6];

function calculaAprovacao($nota){
    return $nota >= 60 ? "Aprovado" : "Reprovado";
}
include "cabecalho.php"
?>

<table border="1px">
     <tr>
        <th>Matemática</th>
        <td><?php echo calculaAprovacao($notas[0]); ?></td>
    </tr>
    <tr>
        <th>Português</th>
        <td><?php echo calculaAprovacao($notas[1]); ?></td>
    </tr>
    <tr> 
        <th>Química</th>
        <td><?php echo calculaAprovacao($notas[2]); ?></td>
    </tr>

     <tr>
            <th>Física</th>
            <td><?php echo calculaAprovacao($notas[3]); ?></td>
        <tr>

        <tr>
            <th>Geografia</th>
            <td><?php echo calculaAprovacao($notas[4]); ?></td>
        <tr>

    <tr>
        <th>História</th>
        <td><?php echo calculaAprovacao($notas[5]); ?></td>
    </tr>
</table>
</body>
</html>

If you really want to use foreach you can do it and even recommend you do it this way. But you need to change the code a bit, it would look like this:

<?php            
$notas = array( "Matemática" => $_POST["matematica"], "Português" => $_POST["portugues"],
                "Química" => $_POST["quimica"], "Física" => $_POST["fisica"], 
                "Geografia" => $_POST["geografia"], "História" => $_POST["historia"]);

function calculaAprovacao($nota){
    return $nota >= 60 ? "Aprovado" : "Reprovado";
}
include "cabecalho.php"
?>
<table border="1px">
<?php foreach($notas as $disciplina => $nota) { ?>
     <tr>
        <th><?php echo $disciplina; ?></th>
        <td><?php echo calculaAprovacao($nota); ?></td>
    </tr>
<?php } ?>
</table>
</body>
</html>

You can do this in a better way, but you would probably have to change the structure of your application, it would not be enough to just change this piece of code.

And something tells me that this <th> is in the wrong place too, which would need to change this HTML code.

    
06.03.2015 / 19:36
3

What you're doing wrong is that foreach iterates over an array and the return of your calculaAprovacao method is a boolean.

If you want to use this way, you have to create an array inside the method and add true or false to each iteration within the method and return the array.

function calculaAprovacao($nota){
    $resultados = array();
    foreach($nota as $lista){
        $resultados[] = ($lista >= 60) ? "Aprovado" : "Reprovado";
    }
    return $resultados;
}

You will need to change the print call too, for example:

<th>Matemática</th>
<td><?php echo $aprovacao; ?></td>
    
06.03.2015 / 19:20
2

You do not need to use foreach to generate this table.

You can do this:

<?php
function calculaAprovacao($notas) {
    // "resultados" retornará um array contendo "Aprovado" ou "Reprovado" 
    // nos índices na mesma ordem do parâmetro "$notas"
    $resultados = [];

    foreach($notas as $nota) {
        $resultados[] = ($nota >= 60 ? "Aprovado" : "Reprovado");
    }

    return $resultados;
}

In your HTML it would only print the items of the returned array:

<table border="1px">
    <?php $aprovacao = calculaAprovacao($notas); ?>
    <tr>
        <th>Matemática</th>
        <td><?php echo $aprovacao[0]; ?></td>
    </tr>
    <tr>
        <th>Português</th>
        <td><?php echo $aprovacao[1]; ?></td>
    </tr>
    <tr> 
        <th>Química</th>
        <td><?php echo $aprovacao[2]; ?></td>
    </tr>

     <tr>
            <th>Física</th>
            <td><?php echo $aprovacao[3]; ?></td>
        <tr>

        <tr>
            <th>Geografia</th>
            <td><?php echo $aprovacao[4]; ?></td>
        <tr>

    <tr>
        <th>História</th>
        <td><?php echo $aprovacao[5]; ?></td>
    </tr>
</table>
    
06.03.2015 / 19:36