Php showing only one database value

Asked

Viewed 59 times

0

php is showing only 1 value in the tables, I needed it to show all, one below the other.

<?php require 'pages/header.php'; ?>    
<?php
    if(empty($_SESSION['cLogin'])) {
        ?>
        <script type="text/javascript">window.location.href="login.php";</script>   
        <?php
        exit;
}
?> 
<div class="container">
    <h1>Controle de Clientes | Agro Grama</h1>
    <a href="add-clientes.php" class="btn btn-success">Adicionar Clientes</a>
    <table class="table table-striper">
        <thead>
            <tr>
                <th>Nome</th>
                <th>E-mail</th>
                <th>Telefone</tb>
                <th>Endereço</th>
                <th>Cidade</tb>
                <th>Firma</th>
                <th>CNPJ</th>
                <th>CPF</th>
            </tr>
        </thead>
        <tr>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT nome AS nome FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->nome); 

                    ?></th>

            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT email AS email FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->email); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT telefone AS telefone FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->telefone); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT endereco AS endereco FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->endereco); 

                    ?>
            </td> 
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cidade AS cidade FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cidade); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT firma AS firma FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->firma); 

                    ?>
            </td>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cnpj AS cnpj FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cnpj); 

                    ?>
            <td>
                <?php
                    $sql = $pdo->prepare("SELECT cpf AS cpf FROM clientes");
                    $sql->execute();

                    $ln = $sql->fetchObject();

                    echo ($ln->cpf); 

                    ?>
            </td>

        </tr>
        <tr>
        </tr>   
    </table>
</div>
<?php require 'pages/footer.php'; ?>
  • 1

    You are making a query for each field(8 in total), what you need is a while and for each row show the various fields

  • Sorry, I’m new to PHP, but you can answer my code with the while for me to see?

1 answer

1

In your example instead of doing a query for each field you want to show, you should change the query to return all fields and iterate over the returned records:

<?php
$sql = $pdo->prepare("SELECT nome, email, telefone, endereco, cidade, firma, cnpj FROM clientes");
$sql->execute();
foreach ($sql->fetchAll() as $row) {
    ?>
    <tr>
        <td><?php echo $row['nome']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['telefone']; ?></td>
        <td><?php echo $row['endereco']; ?></td>
        <td><?php echo $row['cidade']; ?></td>
        <td><?php echo $row['firma']; ?></td>
        <td><?php echo $row['cnpj']; ?></td>
    </tr>
    <?php
}
?>
  • Good @Isac, already changed!

  • Opaa, thank you very much.

Browser other questions tagged

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