Display data in a foreach table

Asked

Viewed 12,476 times

1

I’m having problems on the results page, the others are working correctly, the goal is to print the students' approval or failure 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>
  • The question is how to fill the table with the results of approved or disapproved with foreach using the function, someone can help me ?

3 answers

4


The use of foreach doesn’t seem to make sense there. in HTML you’re repeating all 6 lines (<tr>) then there’s no point in using the foreach. And then there’s no point in using the foreach within the function that picks up the approval text, since it is running 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 a proper code. I will show the two forms.

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>

I put in the Github for future reference.

If you really want to use the foreach have how to do and even recommend to do this way. But need to change a little the code, would be 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>

I put in the Github for future reference.

You can do this even better but you probably would have to change the structure of your application, not just change this piece of code.

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

  • Thanks, it worked great. That’s exactly what I thought.

3

What you’re doing wrong is that foreach iterates over an array and the return of its method calculaAprovacao is a Boolean.

If you want to use it this way, you will need to create an array within the method and add true or false for 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 as well, for example:

<th>Matemática</th>
<td><?php echo $aprovacao; ?></td>
  • Luis in this case takes only the value of approved, need to print approved or disapproved in the table, it is possible to facilitate the way I put the table inside the foreach ?

  • @Thiago updated the code, I had forgotten that it was a message "personalized".

  • Beauty was worth man.

2

You don’t need to use foreach to generate this table.

You can do it like 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 just 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>
  • Thank you worked out here.

Browser other questions tagged

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