Problem storing data in Array

Asked

Viewed 524 times

1

I’m creating a program that stores a person’s tasks in an array, and shows them these tasks. But I’m having trouble showing the tasks. I can’t show the old tasks. Only the last task typed is shown.

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>

Grateful for the attention!

2 answers

4


Basically there’s no way the code can work a miracle and show you something you’re not saving anywhere.

When you do that:

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

is changing the variable for a new.

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

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


Wipe your code a little bit:

<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.

2

for this simple example, you don’t need Sessions, php stores values in arrays via GET requests as well, 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>

Browser other questions tagged

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