print the lines according to the array

0

How to print this array of my files this way:

  

NAME | PHONE | EMAIL | FAVORITE BIRTHDAY | DESCRIPTION

I can only print by columns

follow the code

    <!DOCTYPE html>
<?php session_start(); ?>
<html lang="pt-br">
    <head>
        <meta charset="UTF-8">
        <title>Lista de Contatos</title>
    </head>
    <body>
        <form>
            <fieldset>
                <legend>Lista de Contatos</legend>
                Nome<br>
                <input type="text" name="nome">
                <br>
                Telefone<br>
                <input type="tel" name="telefone">
                <br>
                Email<br>
                <input type="email" name="email">
                <br>
                Data de nascimento<br>
                <input type="date" name="dataNascimento">
                <br>
                Favorito <input type="checkbox" name="vip" value="Favorito">
                <br>
                Descrição<br>
                <textarea name="descricao" rows="10" cols="30">
                </textarea>
                <br>
                <input type="submit" value="Cadastrar">
            </fieldset>
        </form>
        <?php
            if(isset($_GET['nome'])){
                $_SESSION['l1'][] = $_GET['nome'];
            }
            if(isset($_GET['telefone'])){
                $_SESSION['l1'][] = $_GET['telefone'];
            }
            if(isset($_GET['email'])){
                $_SESSION['l1'][] = $_GET['email'];
            }
            if(isset($_GET['dataNascimento'])){
                $_SESSION['l1'][] = $_GET['dataNascimento'];
            }
            if(isset($_GET['vip'])){
                $_SESSION['l1'][] = $_GET['vip'];
            }
            if(isset($_GET['descricao'])){
                $_SESSION['l1'][] = $_GET['descricao'];
            }


            if(isset($_SESSION['l1'])){
                $lista_contatos = array();
                $lista_contatos = $_SESSION['l1'];
                ?>
                <table border="1">
                    <tr>
                        <th colspan="6">Contatos Cadastrados</th>
                    </tr>
                    <tr>
                        <td>Nome</td>
                        <td>Telefone</td>
                        <td>Email</td>
                        <td>Data de Nascimento</td>
                        <td>Favorito</td>
                        <td>Descrição</td>
                    </tr>


                        <?php foreach($lista_contatos as $contatos) : ?>
                            <tr>
                                <td><?php echo $contatos; ?></td>

                        <?php endforeach?>


                </table>
        <?php } ?>
    </body>
</html>
    
asked by anonymous 23.08.2015 / 03:25

2 answers

0

If you want to print them in this sequence, why not use the implode function?

If you understand that you want to print the indexes, not the values, do the following:

echo implode('|', array_keys($lista_contatos));

This function is responsible for "joining" the elements of a array , to transform them into a single string .

In turn, array_keys , will return a array with all indices used in its $lista_contatos .

    
24.08.2015 / 17:09
-1
$contato = array();
if(isset($_GET['nome'])){
  $contato['nome'] = $_GET['nome'];
}
if(isset($_GET['email'])){
  $contato['email'] = $_GET['email'];
}
...

$_SESSION['l1'][] = $contato; 

...
<?php foreach($lista_contatos as $contato) : ?>
    <tr>                            
        <td><?php echo $contato['nome']; ?></td>
        <td><?php echo $contato['telefone']; ?></td>
        <td><?php echo $contato['email']; ?></td>
        ....
    </tr>
    <?php endforeach; ?>
    
23.08.2015 / 23:58