print the lines according to the array

Asked

Viewed 462 times

0

How to print my array this way:

NAME|TELEPHONE|EMAIL|FAVORITE DATE OF BIRTH|DESCRIPTION

I can only print by columns

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

2 answers

0

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

If I understand that you want to print the indexes, not the values, do it as follows:

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

This function is responsible for "uniting" the elements of a array, to turn them into one string.

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

-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; ?>
  • 3

    I don’t understand your answer, there’s only code?

Browser other questions tagged

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