How to get mysql data for a shopping cart

1

Well I would like to know if there is a better method than this: it looks for everything that has skin_type == 2, and I wanted to do several types to sell, would I have a way to get item by item and by in a div or just like that?

 <?php
                $consulta = $pdo->prepare("SELECT * FROM homesite");
                $consulta -> execute();

                $linhas = $consulta -> rowCount();

                foreach($consulta as $mostra):
            ?>
    <div class="card">
        <?php
if ($mostra['skin_tipo'] == 2) {?>
      <img class="skin"src="images/<?= $mostra['skin_img']?>" alt="Nome da Empresa: 
      <?= $mostra['skin_nome']?>" title="Nome da Empresa: <?= $mostra['skin_nome']?>">
      <div class="info">
      <h1 class="color-white text-center font-text-light-med font-weight-heavy bgcolor-black"><?= $mostra['skin_nome']?></h1> 
      </div>
      <p class="preccor">Por: R$ <?= number_format($mostra['skin_preco'], 2,',','.')?></p>
      <button class="bgcolor-red text-center btn"><a href="comprar.php?prod=<?= $mostra['id_skin']?>">Comprar</a></button>
        <?php } ?>
    </div>
    <?php endforeach; ?>
    
asked by anonymous 01.10.2018 / 23:07

1 answer

1
  • Bring in the query only those that have skin_tipo=2

    $consulta = $pdo->prepare("SELECT * FROM homesite WHERE skin_tipo=2");
    
  • Change these lines

      $linhas = $consulta -> rowCount();
      foreach($consulta as $mostra):
    

    by these

      $fetchAll = $consulta->fetchAll(); 
      foreach($consulta as $mostra):
    
  • Code

    $consulta = $pdo->prepare("SELECT * FROM homesite WHERE skin_tipo=2"); 
    $consulta -> execute(); 
    
    $fetchAll = $consulta->fetchAll(); 
    
    foreach($consulta as $mostra): 
    
      // o que quer seja apresentado
    
    endforeach; 
    
        
    02.10.2018 / 05:20