0

Estou tentando executar o código abaixo, mas está retornando o erro:

Warning: Illegal string offset 'nome' tarefas.php on line 89

<?php
        session_start();
        if (isset($_GET['nome']) && $_GET['nome'] != '') {
            $tarefa = array();
            $tarefa['nome'] = $_GET['nome'];

             if (isset($_GET['descricao'])) {
                $tarefa['descricao'] = $_GET['descricao'];
             }else{
                $tarefa['descricao'] = '';
             } 

             if (isset($_GET['prazo'])) {
                 $tarefa['prazo'] = $_GET['prazo'];
             }else{
                 $tarefa['prazo'] = '';
             }

              $tarefa['prioridade'] = $_GET['prioridade'];

             if (isset($_GET['concluida'])) {
                 $tarefa['concluida'] = $_GET['concluida'];
             }else{
                 $tarefa['concluida'] = '';
             } 

             $_SESSION['lista_tarefas'][] = $tarefa;             
        }

        if (isset($_SESSION['lista_tarefas'])) {
            $lista_tarefas = $_SESSION['lista_tarefas'];
        }else{
            $lista_tarefas = array();
        }
        ?>

    <html>
        <head>
            <meta charset="utf-8" />
            <title>Gerenciador de Tarefas</title>
            <link rel="stylesheet" href="estilos.css" type="text/css" />
        </head>
    <body>
        <h1>Gerenciador de Tarefas</h1>
        <form>
            <fieldset>
                <legend>Nova tarefa</legend>
                <label>
                    Tarefa:
                    <input type="text" name="nome" />
                </label>
                <label>
                    Descrição ( Opcional)
                    <textarea name="descricao"></textarea>
                </label>
                <label>
                    Prazo (Opcional)
                    <input type="text" name="prazo">                
                </label>

            <fieldset>
                <legend>Prioridade</legend>
                <label>
                    <input type="radio" name="prioridade" value="Baixa" checked />
                    Baixa
                    <input type="radio" name="prioridade" value="Média" />
                    Média
                    <input type="radio" name="prioridade" value="Alta" />
                    Alta
                </label>
            </fieldset>
                <label>
                    Tarefa Concluída:
                    <input type="Checkbox" name="concluida" value="sim" />
                </label>
                <input type="submit" value="Cadastrar">
            </fieldset>       
        </form>
        <table>
            <tr>
                <th>Tarefa</th>
                <th>Descrição</th>
                <th>Prazo</th>
                <th>Prioridade</th>
                <th>Concluida</th>
            </tr>        
            <?php foreach ($lista_tarefas as $tarefa) : ?>
            <tr>
                <td><?php echo $tarefa['nome']; ?> </td>
                <td><?php echo $tarefa['descricao']; ?> </td>
                <td><?php echo $tarefa['prazo']; ?></td>
                <td><?php echo $tarefa['prioridade']; ?></td>
                <td><?php echo $tarefa['concluida']; ?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </body>
    </html>

1 Resposta 1

2

Fiz ajuste nesse código principalmente no foreach aonde acontecia tal erro

<?php
    session_start();
    if (isset($_GET['nome']) && $_GET['nome'] != '') {
        $tarefa = array();
        $tarefa['nome'] = $_GET['nome'];

         if (isset($_GET['descricao'])) {
            $tarefa['descricao'] = $_GET['descricao'];
         }else{
            $tarefa['descricao'] = '';
         } 

         if (isset($_GET['prazo'])) {
             $tarefa['prazo'] = $_GET['prazo'];
         }else{
             $tarefa['prazo'] = '';
         }

          $tarefa['prioridade'] = $_GET['prioridade'];

         if (isset($_GET['concluida'])) {
             $tarefa['concluida'] = $_GET['concluida'];
         }else{
             $tarefa['concluida'] = '';
         } 

         $_SESSION['lista_tarefas'][] = $tarefa;             
    }

    if (isset($_SESSION['lista_tarefas'])) {
        $lista_tarefas = $_SESSION['lista_tarefas'];
    }else{
        $lista_tarefas = array();
    }
?>

    <html>
        <head>
            <meta charset="utf-8" />
            <title>Gerenciador de Tarefas</title>
            <link rel="stylesheet" href="estilos.css" type="text/css" />
        </head>
    <body>
        <h1>Gerenciador de Tarefas</h1>
        <form>
            <fieldset>
                <legend>Nova tarefa</legend>
                <label>
                    Tarefa:
                    <input type="text" name="nome" />
                </label>
                <label>
                    Descrição ( Opcional)
                    <textarea name="descricao"></textarea>
                </label>
                <label>
                    Prazo (Opcional)
                    <input type="text" name="prazo">                
                </label>

            <fieldset>
                <legend>Prioridade</legend>
                <label>
                    <input type="radio" name="prioridade" value="Baixa" checked />
                    Baixa
                    <input type="radio" name="prioridade" value="Média" />
                    Média
                    <input type="radio" name="prioridade" value="Alta" />
                    Alta
                </label>
            </fieldset>
                <label>
                    Tarefa Concluída:
                    <input type="Checkbox" name="concluida" value="sim" />
                </label>
                <input type="submit" value="Cadastrar">
            </fieldset>       
        </form>
        <table>
            <tr>
                <th>Tarefa</th>
                <th>Descrição</th>
                <th>Prazo</th>
                <th>Prioridade</th>
                <th>Concluida</th>
            </tr>        
            <?php if (isset($lista_tarefas) && is_array($lista_tarefas) && sizeof($lista_tarefas) > 0): ?>
            <?php foreach ($lista_tarefas as $tarefa) : ?>
            <tr>
                <td><?php echo isset($tarefa['nome']) ?  $tarefa['nome'] : ''; ?> </td>
                <td><?php echo isset($tarefa['descricao']) ?  $tarefa['descricao'] : ''; ?> </td>
                <td><?php echo isset($tarefa['prazo']) ? $tarefa['prazo'] : ''; ?></td>
                <td><?php echo isset($tarefa['prioridade']) ? $tarefa['prioridade'] : ''; ?></td>
                <td><?php echo isset($tarefa['concluida']) ? $tarefa['concluida'] : ''; ?></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </table>
    </body>
</html>

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .