0
Hello, all good?
I’m having a question.
I want to list, a list of contacts, but I can’t echo in the array, you could help me?
that’s my code:
<?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)) {
$contatos['nome'][] = $_GET['nome'];
}
if (array_key_exists('tel', $_GET)) {
$contatos['tel'][] = $_GET['tel'];
}
if (array_key_exists('email',$_GET)) {
$contatos['email'][] = $_GET['email'];
}
?>
<table border="1">
<tr>
<th>Nome</th>
<th>telefone</th>
<th>E-mail</th>
</tr>
<?php foreach($contatos as $contato) : ?>
<?php print_r($contato); ?>
<tr>
<td><?php echo $nome; ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
I am not able to identify the problem in foreach, I tried several times at first I just want to list the name, because if I can list the name, the other fields will follow the same logic.
If you want to display only what is sent via $_GET, I don’t see the need to use array, just: .... if (array_key_exists('name', $_GET)) { $name = $_GET['name']; } .... <td><? php echo $name; ></td>
– Marcos Xavier