Return data from my table by formatting in html

Asked

Viewed 839 times

0

I have a function within my class to list data; below follows the code:

public function listadados(){

       try{
           //retornar um array
           $sql = "SELECT * FROM web_cadcli";
           $lista = $this->con->conectar()->prepare($sql);
           $lista->execute();
           $retDados = array ( $lista-> fetchAll(PDO::FETCH_ASSOC));
           print_r($retDados);
       }catch(PDOException $erro_2){
           echo 'erro'.$erro_2->getMessage();       
       }
}

I want to put the return that is below. within an html

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [idcad_cliente] => 1
                    [nm_cliente] => Rodrigo Zanetti
                    [email_cliente] => [email protected]
                    [senha_cliente] => 7ik3ikelek
                    [id_identpess] => 1
                    [img] => 
                )

            [1] => Array
                (
                    [idcad_cliente] => 2
                    [nm_cliente] => Rodrigo Zanetti
                    [email_cliente] => [email protected]
                    [senha_cliente] => k3ik3ik3ikkejeh
                    [id_identpess] => 1
                    [img] => 
                )

            [2] => Array
                (
                    [idcad_cliente] => 3
                    [nm_cliente] => Adriana Silva Souto
                    [email_cliente] => [email protected]
                    [senha_cliente] => k3ikeikeikeieçeoel
                    [id_identpess] => 1
                    [img] => 
                )

This screen below is my template, where I want to return the formatted values. name below NAME: email below EMAIL, as a table.

listed(); ?>
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr class="active">
                    <th>Nome</th>
                    <th>E-mail</th>
                    <th>Editar</th>
                   <th>Deletar</th>
                </tr>
            </thead>
            <tbody>
            </table>

  • Why don’t you do return $retDados in its method and use the foreach to browse them, displaying in HTML?

1 answer

1


Maybe this is the solution :

Listed method()

 public function listadados(){

       try{
           //retornar um array
           $sql = "SELECT * FROM web_cadcli";
           $lista = $this->con->conectar()->prepare($sql);
           $lista->execute();
           $retDados = $lista-> fetchAll(PDO::FETCH_ASSOC);

           return $retDados;

       }catch(PDOException $erro_2){
           echo 'erro'.$erro_2->getMessage();       
       }
}

So the array with the records will be returned and can be used as follows.

<?php
  $objeto = new NomeDaClasse;
  $clientes = NomeDaClasse->listadados();
?>

<table class="table table-striped table-bordered table-hover">
            <thead>
                <tr class="active">
                    <th>Nome</th>
                    <th>E-mail</th>
                    <th>Editar</th>
                   <th>Deletar</th>
                </tr>
            </thead>
            <tbody>
               <?php foreach($clientes as $cliente) : ?>
                   <tr>
                     <td><?=$cliente['nm_cliente']?></td>
                     <td><?=$cliente['email_cliente']?></td>
                     <td><a href="?id_cliente=<?=$cliente['idcad_cliente']?>&action=editar">Editar</a></td>
                     <td><a href="?id_cliente=<?=$cliente['idcad_cliente']?>&action=deletar">Deletar</a></td>
                   </tr>
               <?php endforeach; ?>
            </tbody>
            </table>

With the Return data can be passed to a variable and be traversed by a loop foreach.

That part of Edit and Delete I set the parameters assuming that the $_GET since you didn’t say anything about that part.

  • It worked perfect as I needed it. Thank you.

  • @Rodrigozanetti consider accepting the answer. See: https://pt.meta.stackoverflow.com/a/1079/3117

Browser other questions tagged

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