foreach HTML table

Asked

Viewed 183 times

-4

I am trying again to create a table with the entered data using

$_SESSION

    <?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)) {
                $_SESSION[contatos]['nome'][] = $_GET['nome'];
             }
            if (array_key_exists('tel', $_GET)) {
                $_SESSION[contatos]['tel'][] = $_GET['tel'];
            }
            if (array_key_exists('email',$_GET)) {
                $_SESSION[contatos]['email'][] = $_GET['email'];
            }
            $contatos = [];
            if (array_key_exists('contatos',$_SESSION)) {
                $contatos = $_SESSION['contatos'];
            }
    ?>

    <table border="1">
        <tr>
            <th>Nome</th>
            <th>telefone</th>
            <th>E-mail</th>
        </tr>
        <?php foreach($contatos as $contato=>$value) : ?>

        </tr> 
            <?php for($i=0; $i < count($value); $i++): ?>

                    <td><?php echo $value[$i]; ?></td>

            <?php endfor; ?>
        </tr>              

        <?php endforeach; ?>

    </table>
</body>

</html>

is storing normal, but is not following the order of the table. tabela

  • See message in $_SESSION[contatos] the contatos is not set. If it is to be a variable, it lacked the $ in front, but does it make sense to have an empty array as the session key? If it was supposed to be string, the quotes were missing

  • I removed the empty contacts, but still it is not following the order of the table; It is getting all in one column.

1 answer

0


<?php 
session_start();
//unset($_SESSION['contatos']);
?>
<!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)) {
            $_SESSION['contatos']['nome'][] = $_GET['nome'];
         }
        if (array_key_exists('tel', $_GET)) {
            $_SESSION['contatos']['tel'][] = $_GET['tel'];
        }
        if (array_key_exists('email',$_GET)) {
            $_SESSION['contatos']['email'][] = $_GET['email'];
        }

        if (array_key_exists('contatos',$_SESSION)) {
            $contatos = $_SESSION['contatos'];
        }
?>

<table border="1">
    <tr>
        <th>Nome</th>
        <th>telefone</th>
        <th>E-mail</th>
    </tr>
    <?php for($i=0; $i < count($contatos['nome']); $i++): ?>
        <tr> 
            <td><?php echo $contatos['nome'][$i];?></td>
            <td><?php echo $contatos['tel'][$i];?></td>
            <td><?php echo $contatos['email'][$i];?></td>
        </tr>    
    <?php endfor;?>
</table>

  • It hasn’t worked yet, it’s still in the same column. I tried to leave the <tr> out of the loop and it didn’t work either, so I keep searching. Thanks for your time.

  • @Carlos1br tries now, I redid the code

  • Now it worked, and I understood the logic! Thank you very much!

Browser other questions tagged

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