Error including Session or foreach in PHP code

Asked

Viewed 86 times

0

I am studying PHP with a book I bought and the exercise is to create a task list. The browser is returning me the following error:

Notice: Undefined variable: lista_tarefas in C: Users Elton Desktop Cadastro root Listadetarefas template.php on line 26

Warning: Invalid argument supplied for foreach() in C: Users Elton Desktop Cadastre root Listadetarefas template.php on line 26 Tasks

The error snippet in template.php is this code:

<table>
    <tr>
       <th>Tarefas</th>
    </tr>
    <?php foreach($lista_tarefas as $tarefa) : ?>
    <tr>
       <td><?php echo $tarefa; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

And the PHP code I’m using is this:

<?php 

session_start();

    if (array_key_exists('nome', $_GET)){ 
        $_SESSION['lista_tarefas'][] = $_GET['nome'];
    }
    $lista_tarefas = [];
            
    if (array_key_exists('lista_tarefas', $_SESSION)){
        $lista_tarefas  = $_SESSION['lista_tarefas'];
    }

?>

Please help me understand where I’m going wrong. I couldn’t get to the error.

  • Adds to URL that you are using to access the file so that we can analyze it better, because your file is receiving parameters via GET.

  • These two code snippets are in the same file or are separate files?

  • http://localhost:8079/Listadetarefas/template.php? nome=d

  • Are in separate files

2 answers

0


How are you using separate files in this foreach there will be no variable $lista_tarefas, for she was not created:

<?php
    foreach ($lista_tarefas as $tarefa):
        ...
    endforeach;

That’s the reason for the mistake:

Notice: Undefined variable: lista_tarefas in C: Users Elton Desktop Cadastro root Listadetarefas template.php on line 26

I believe you would have to add this code snippet before the foreach in the same file (at the top of the page, before the code HTML is the most suitable place):

<?php
    session_start();

    if (array_key_exists('nome', $_GET)){ 
        $_SESSION['lista_tarefas'][] = $_GET['nome'];
    }
    $lista_tarefas = [];

    if (array_key_exists('lista_tarefas', $_SESSION)){
        $lista_tarefas  = $_SESSION['lista_tarefas'];
    }
  • 1

    It worked fine. I managed to get to the point I wanted with this support, thank you!

0

<?php
session_start();


if (array_key_exists('nome', $_GET)){ 
    $_SESSION['lista_tarefas'][] = $_GET['nome'];
}
$lista_tarefas = [];

if (array_key_exists('lista_tarefas', $_SESSION)){
    $lista_tarefas  = $_SESSION['lista_tarefas'];
}


include './template.php';

Your code needs to look like this, as the top friend said, you need to include html inside php, or php inside html, the best thing to do is to include the file that has php inside the template file, but as I don’t know which file is I did the other way around, will work the same way, only q you will need to run the php file and not the template.php

  • Thanks man. Your help was great value!

Browser other questions tagged

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