Save variables in PHP PDO foreach cycle

Asked

Viewed 123 times

-1

I’m making a web app for gaming sales. I did a foreach cycle to display the games on the main page.

<?php


          $stmt = $conn->query('SELECT * FROM JOGO');
          foreach ($stmt as $row){
            $idJ = $row['id_jogo']; 
            $_SESSION['id_jogo'.$row.''] = $idJ;
            echo '<link href="home/vendor/bootstrap/css/bootstrap.min.php" rel="stylesheet">';
            echo '<link href="style.php" rel="stylesheet">';
            echo  '
                  <div class="col-lg-4 col-md-6 mb-4" >   
                    <div class="card h-100 ">  
                      <a href="itempage.php"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
                      <div class="card-body">
                        <h4 class="card-title">
                          <a href="#">  ' .$row["nome"].' ' .$_SESSION['id_jogo'.$row.''].' </a>
                        </h4>
                        <h5>' .$row["preco"].'€ </h5>
                        <p class="card-text">' .$row["descricao"].' </p>
                      </div>
                      <div class="card-footer">
                        <small class="text-muted">Vendedor: </small>
                      </div> 
                    </div>
                  </div> ' ;  
             }
        ?>

When I press a game I go to the item page for details. But as only the id of the last game of the cycle is saved only its dice. My question is how can I store the variables of the various items that go through the cycle ?

1 answer

0

You can keep the objeto or array() within the $_SESSION thus: $_SESSION['jogo'][$id] = $row;

        echo '<link href="home/vendor/bootstrap/css/bootstrap.min.php" rel="stylesheet">';
        echo '<link href="style.php" rel="stylesheet">';

          $stmt = $conn->query('SELECT * FROM JOGO');

          foreach ($stmt as $row){

            $idJ = $row['id_jogo']; 

            $_SESSION['id_jogo'][ $idJ ] = $row;

            echo  '
                  <div class="col-lg-4 col-md-6 mb-4" >   
                    <div class="card h-100 ">  
                      <a href="itempage.php"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
                      <div class="card-body">
                        <h4 class="card-title">
                          <a href="#">  ' .$row["nome"].' ' .$_SESSION['id_jogo'][ $idJ ].' </a>
                        </h4>
                        <h5>' .$row["preco"].'€ </h5>
                        <p class="card-text">' .$row["descricao"].' </p>
                      </div>
                      <div class="card-footer">
                        <small class="text-muted">Vendedor: </small>
                      </div> 
                    </div>
                  </div> ' ;  
             }

Browser other questions tagged

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