-4
I am trying again to create a table with the entered data using
$_SESSION
<?php session_start() ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<fieldset>
<legend>Lista de contatos</legend>
<label for="name">
Nome:
<input type="text" name="nome" id="name">
</label><br>
<label for="tel">
telefone:
<input type="tel" name="tel" id="tel">
</label><br>
<label for="email">
E-mail:
<input type="email" name="email" id="email">
</label>
<input type="submit" value="Cadastrar">
</fieldset>
</form>
<?php
$contatos = [];
if (array_key_exists('nome', $_GET)) {
$_SESSION[contatos]['nome'][] = $_GET['nome'];
}
if (array_key_exists('tel', $_GET)) {
$_SESSION[contatos]['tel'][] = $_GET['tel'];
}
if (array_key_exists('email',$_GET)) {
$_SESSION[contatos]['email'][] = $_GET['email'];
}
$contatos = [];
if (array_key_exists('contatos',$_SESSION)) {
$contatos = $_SESSION['contatos'];
}
?>
<table border="1">
<tr>
<th>Nome</th>
<th>telefone</th>
<th>E-mail</th>
</tr>
<?php foreach($contatos as $contato=>$value) : ?>
</tr>
<?php for($i=0; $i < count($value); $i++): ?>
<td><?php echo $value[$i]; ?></td>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
is storing normal, but is not following the order of the table.
See message in
$_SESSION[contatos]
thecontatos
is not set. If it is to be a variable, it lacked the $ in front, but does it make sense to have an empty array as the session key? If it was supposed to be string, the quotes were missing– Woss
I removed the empty contacts, but still it is not following the order of the table; It is getting all in one column.
– Carlos1br