Warning: Invalid argument supplied for foreach () in C: \ xampp \ htdocs \ tccSistreinamento \ view \ modules \ student \ tmp \ listAluno.php on line 10

-2

I need help with this code, it does not return the list with the array of fields, I've done everything, someone can help, it's my job to desperate, nothing works

  

file listaluno.php

<?php
extract($_POST);

$genList = json_decode($obj);
echo($genList)


?>
<table class= "table table-striped">
    <tbody> 
        <thead>

        </thead>

        <?php foreach($genList as $aluno){ ?>

        <?php 
        $scampos = $aluno->StrNome. "|".$aluno->NumMatricula. "|" .$aluno->DtNascimento. "|" .$aluno->NumIdUop; 
        ?>

        <tr campos="<?php echo $scampos; ?>">

            <td><?php echo $aluno->StrNome; ?></td>
            <td><?php echo $aluno->NumMatricula; ?></td>
            <td><?php echo $aluno->DtNascimento; ?></td>
            <td><?php echo $aluno->NumIdUop; ?></td>                        
            <td><a href="#" class="editar_aluno"><span class="glyphicon glyphicon-pencil" title="Editar Aluno"></span></a></td>
            <td><a href="#" class="excluir_aluno"><span class="btTabela glyphicon glyphicon-trash" title="Excluir Aluno"></span></a></td>
        </tr>
    <?php } ?>

    </tbody>
</table>

error

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\tccSistreinamento\view\modulos\aluno\tmp\listAluno.php on line 10
    
asked by anonymous 14.10.2017 / 05:03

1 answer

1

Probably the data passed in json_decode is not a valid JSON format string, so there must have been an error when decoding and will not work.

If the variable $obj is a string, it must contain a format such as:

$obj = '{...}';

If it came from a file probably the file read is invalid or it is in the wrong address, if $obj came from extract($_POST); this may be a flaw in your HTML form, anyway the problem is the JSON format.

And another detail, echo does not display the object, only displays number and strings or __toString , I recommend that you use var_dump

Sample test that works

I suppose the form should look like this:

<form action="listaluno.php" method="POST">
    <textarea name="obj">
    [
        {
            "StrNome": "Nome",
            "NumMatricula": 10000,
            "DtNascimento": "10/10/2001",
            "NumIdUop": 1
        },
        {
            "StrNome": "Foo Bar",
            "NumMatricula": 3000,
            "DtNascimento": "10/10/2002",
            "NumIdUop": 2
        }
    ]
    </textarea>
    <button>Enviar</button>
</form>

And then try clicking Send , it should work, the test resulted in:

  

JSONErrors

NotethattheremayhavebeenafailureintheformatofyourJSON,suchasanextracomma:

[{"StrNome": "Nome",
        "NumMatricula": 10000,
        "DtNascimento": "10/10/2001",
        "NumIdUop": 1
    },
    {
        "StrNome": "Foo Bar",
        "NumMatricula": 3000,
        "DtNascimento": "10/10/2002",
        "NumIdUop": 2
    }, <-------------------- Virgula extra
]

If there is any failure and do this:

<?php

$foo = '[
    {
        "StrNome": "Nome",
        "NumMatricula": 10000,
        "DtNascimento": "10/10/2001",
        "NumIdUop": 1
    },
    {
        "StrNome": "Foo Bar",
        "NumMatricula": 3000,
        "DtNascimento": "10/10/2002",
        "NumIdUop": 2
    },
]';

var_dump(json_decode($foo));

You will return this:

  

NULL

How to Debug JSON

If you are completely sure that your json was in the correct format, then redo your script like this:

<?php
extract($_POST);

$genList = json_decode($obj);

if (!$genList) {
    switch (json_last_error()) {
        case JSON_ERROR_DEPTH:
            echo 'A profundidade máxima da pilha foi excedida';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo 'JSON malformado ou inválido';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo 'Erro de caractere de controle, possivelmente codificado incorretamente';
        break;
        case JSON_ERROR_SYNTAX:
            echo 'Erro de sintaxe';
        break;
        case JSON_ERROR_UTF8:
            echo 'Caractere UTF-8 malformado, codificação possivelmente incorreta';
        break;
        default:
            echo 'Erro desconhecido';
        break;
    }
    exit;
}

var_dump($genList);

?>
<table class= "table table-striped">
    <tbody>
        <thead>

        </thead>

        <?php foreach($genList as $aluno){ ?>

        <?php
        $scampos = $aluno->StrNome. "|".$aluno->NumMatricula. "|" .$aluno->DtNascimento. "|" .$aluno->NumIdUop;
        ?>

        <tr campos="<?php echo $scampos; ?>">

            <td><?php echo $aluno->StrNome; ?></td>
            <td><?php echo $aluno->NumMatricula; ?></td>
            <td><?php echo $aluno->DtNascimento; ?></td>
            <td><?php echo $aluno->NumIdUop; ?></td>
            <td><a href="#" class="editar_aluno"><span class="glyphicon glyphicon-pencil" title="Editar Aluno"></span></a></td>
            <td><a href="#" class="excluir_aluno"><span class="btTabela glyphicon glyphicon-trash" title="Excluir Aluno"></span></a></td>
        </tr>
    <?php } ?>

    </tbody>
</table>
  

Note: I have listed only a few errors, however in PHP7 there are more errors

    
15.10.2017 / 02:36