Problem in storing data in Array

1

I'm creating a program that stores a person's tasks in an array, and shows them those tasks. But I have trouble showing tasks. I can not show the old jobs. Only the last task entered is displayed.

Code:

<?php session_start(); ?>
<html>

    <head>
        <title>Gerenciador de Tarefas</title>
     </head>

    <body>

        <h1>Gerenciador de Tarefas</h1>

    </br>

        <form>
            <fieldset>
                <legend>Nova Tarefa</legend>

                <label>
                Tarefa:
                <input type="text" name="nome"/>
                </label>

                <input type="submit" value="Cadastrar"/>
            </fieldset>
        </form>

    <?php       

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

    $lista_tarefas = array();

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

    <table>
            <tr>
                <th>Tarefas</th>
            </tr>

            <?php foreach($lista_tarefas as $tarefa) : ?>

                <tr>
                    <td><?php echo $tarefa ?> </td>
                </tr>
            <?php endforeach; ?>
        </table>

    </body> 

</html>

Thank you for your attention!

    
asked by anonymous 21.07.2016 / 04:31

2 answers

3

Basically you do not have the code to do a miracle and show something that you are not saving anywhere.

When you do this:

$_SESSION['lista_tarefas'] = $_GET['nome'];

You are changing the variable to a new one.

If you want to store in an array, you need this syntax

$_SESSION['lista_tarefas'][] = $_GET['nome'];


You can wipe your code a little:

<html>
    <head>
       <title>Gerenciador de Tarefas</title>
    </head>
    <body>
       <h1>Gerenciador de Tarefas</h1>
       </br>
       <form>
          <fieldset>
              <legend>Nova Tarefa</legend>
              <label>
                 Tarefa:
                 <input type="text" name="nome"/>
              </label>
              <input type="submit" value="Cadastrar"/>
          </fieldset>
      </form>
<?php       
    session_start();
    $lista_tarefas = isset($_SESSION['lista_tarefas'])?$_SESSION['lista_tarefas']:array();
    if ( isset( $_GET['nome'] ) ) $lista_tarefas[] = $_GET['nome'];
    $_SESSION['lista_tarefas'] = $lista_tarefas;
?>
      <table>
          <tr><th>Tarefas</th></tr>
<?php foreach( $lista_tarefas as $tarefa ) { ?>
          <tr><td><?php echo htmlentities( $tarefa ); ?></td></tr>
<?php } ?>
      </table>
   </body> 
</html>

Remembering that this is a good exercise, but in an actual application you need to store the data somewhere more permanent.

    
21.07.2016 / 05:06
2

For this simple example, you do not need sessions, php stores values in arrays via GET requests too, follow example based on your script. flw;)

<html>

<head>
    <title>Gerenciador de Tarefas</title>
</head>

<body>
    <?php $lista_tarefas = isset($_GET['nomes']) ? $_GET['nomes'] : array(); ?>
    <h1>Gerenciador de Tarefas</h1>
    </br>
    <form>
        <fieldset>
            <legend>Nova Tarefa</legend>
            <label>
            Tarefa:
            <input type="text" name="nomes[]"/>
			<?php foreach($lista_tarefas as $tarefa) : ?>
				<input type="hidden" name="nomes[]" value="<?php echo $tarefa; ?>" />            
                    <?php endforeach; ?>					
            </label>

            <input type="submit" value="Cadastrar" />
        </fieldset>
    </form>

    <table>
        <tr>
            <th>Tarefas</th>
        </tr>

        <?php 
			
		
		foreach($lista_tarefas as $tarefa) : ?>

        <tr>
            <td>
                <?php echo $tarefa ?> </td>
        </tr>
        <?php endforeach; ?>
    </table>

</body>
</html>
    
21.07.2016 / 05:06