Return query with PDO to Bootstrap card

Asked

Viewed 51 times

-1

Good morning to all,

Was using query with mysqli and returning to card in bootstrap, follows code snippet.

<?php
  require_once("conectar.php");
  $comando = "SELECT * FROM agendamentos ORDER BY id DESC";
  $enviar=mysqli_query($conn, $comando);
  $resultado = mysqli_fetch_all($enviar, MYSQLI_ASSOC);
?>

<?php
  foreach ($resultado as $agendamentos ) {
        $id=$agendamentos['id'];
        $user=$agendamentos['user'];
        $telefone=$agendamentos['telefone'];
        $data=$agendamentos['data'];
?>

CARD BOOTSTRAP

<div class="card-deck" style="display: inline-grid;">
    <div class="col-md-offset-1 col-md-6 col-xs-12">
      <div class="card border-primary mb-3" style="width: 21rem;">
          <center>
            <div class="card-header"><strong><?=$user?></strong></div>   
            <div class="card-body text-primary">
              <h5 class="card-title"><strong><?=$data?></strong></h5>
              <p class="card-text">telefone:<br><?=$telefone?></p> 
            </div>
          </center>
        </div>
      </div>
   </div>
<?php
}
?>

I preferred to change my code to PDO and want to ixibir the data in the same way.

So far I was able to list the data, but as I had already said I want the return of the data in the same way I had done with the query msqli.

<?php
        require 'conexao.php';

        $consulta = $pdo->query("SELECT user, data, telefone FROM agendamentos;");

        while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { 
            echo "Empresa: {$linha['user']} - Telefone: {$linha['telefone']} - Data: {$linha['data']}<br />";
        }

        ?>

1 answer

0

There are several ways to return the CARD, one of them (which in my view is the simplest) is to concatenate the result and print everything at the end.

<?php
    require 'conexao.php';

    $consulta = $pdo->query("SELECT user, data, telefone FROM agendamentos;");

    $carBootstrap = '';

    while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { 
        $carBootstrap .= '<div class="card-deck" style="display: inline-grid;">';
        $carBootstrap .= '<div class="col-md-offset-1 col-md-6 col-xs-12">';
        $carBootstrap .= '<div class="card border-primary mb-3" style="width: 21rem;">';
        $carBootstrap .= '<center>';
        $carBootstrap .= '<div class="card-header"><strong>'.$linha['user'].'</strong></div>   ';
        $carBootstrap .= '<div class="card-body text-primary">';
        $carBootstrap .= '<h5 class="card-title"><strong>'.$linha['data'].'</strong></h5>';
        $carBootstrap .= '<p class="card-text">telefone:<br>'.$linha['telefone'].'</p> ';
        $carBootstrap .= '</div>';
        $carBootstrap .= '</center>';
        $carBootstrap .= '</div>';
        $carBootstrap .= '</div>';
        $carBootstrap .= '</div>';
    }

    echo $carBootstrap;
  • thank you so much for the response, I was trying in other ways.

Browser other questions tagged

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