List 3 fields from an Array

Asked

Viewed 81 times

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>

1 answer

0


You have the multidimensional array $contatos that is storing the information nome, tel e email:

if (array_key_exists('chave', $_GET)) {
    $contatos['chave'][] = $_GET['nome'];
}

Thus the foreach($contatos as $key=>$value) this saying that for each contact you have the keys nome, tel e email which in turn contains an array with the information represented by $value:

foreach($contatos as $key=>$value):
    //código
endforeach;

But to access information from $value which is also an array you need another loop or direct access to the keys;

Since I don’t know the demand, what I can do is ask if there’s a need to $contatos["chave"][] be an array?

Resolution:

You can take this array [] of $contatos["chave"] and follow with your code or make a loop

foreach($contatos as $key=>$value):
    for($i=0;$i<count($value);$i++){
        echo $value[$i];
    }
endforeach;
  • 1

    Thank you very much!

Browser other questions tagged

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