Problem with $_SESSION with $_GET displaying in unexpected result table

Asked

Viewed 23 times

1

<?php 

    session_start();

    if(isset($_GET['nome'])){

        $_SESSION['lista'][] = $_GET['nome'];
        $_SESSION['lista'][] = $_GET['telefone'];
        $_SESSION['lista'][] = $_GET['email'];

    }

    $lista = array();

    if(isset($_SESSION['lista'])){

        $lista = $_SESSION['lista'];


    }

?>

...

        <table style="width:100%" border="3">

            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Telefone</th>
                    <th>E-Mail</th>
                </tr>
            </thead>

            <tbody>

                <?php foreach($lista as $lis): ?>
                    <tr>

                        <td><?=$lis?></td>
                        <td><?=$lis?></td>
                        <td><?=$lis?></td>

                    </tr>
                    <?php endforeach; ?>
            </tbody>


        </table>

...

When displaying in the table a line is occupied by names, and the next line by phones, how can I resolve this so that a line has name, phone and email?

  • 1

    Knows the foreach of PHP?

  • Use the [Edit] button to add more information to the question.

  • Yes, I am using the foreach, the problem is in that piece of code, when passing to the array to $_SESSION, which I am not able to do, who can help with code thank.

  • What a piece of code?

  • Yes, but my question is how to store three variables, name, phone and email, in a $_SESSION, and then save and array and display with foreach in the table. Anyone can help ?

  • You’re already doing this. What’s the problem? What is the code that uses foreach? Edit the question and ask it.

  • I edited, placing the foreach part

  • Will be multiple names, phones and email stored in session?

  • Exactly, to each register in the form, new lines with name, telephone and email.

Show 4 more comments

1 answer

1


As will be several records composed by name, phone and email, what you need to do is:

$_SESSION["lista"][] = array(
    "nome" => $_GET["nome"],
    "telefone" => $_GET["telefone"],
    "email" => $_GET["email"]
);

Thus, each item of the session will be one array with the three values. When displaying, use the foreach, as you are already doing, and access the values with named indexes:

$lista = $_SESSION["lista"];

<?php foreach($lista as $lis): ?>
    <tr>
        <td><?= $lis["nome"] ?></td>
        <td><?= $lis["telefone"] ?></td>
        <td><?= $lis["email"] ?></td>
    </tr>
 <?php endforeach; ?>
  • Thank you! worked and learned.

Browser other questions tagged

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