Creating php product page

Asked

Viewed 150 times

0

php and I have the product.php page, when clicking the product on the home page, I want you to redirect to product.php page showing the data of the particular product I clicked, I started the code more is not working, Thank you.

index php.

<?php
      if (is_array($products)) {
        foreach ($products as $id => $row) { ?>
          <div class="col-sm-6 col-md-4 col-lg-3 mb-2">
             <div class="card-deck">
               <div class="card p-2 border-secondary mb-2">
        <a href="product.php?id=<?= $row['product_id'] ?>">
            <div class="hover-image">
                <img class="card-img-top border-secondary" src="images/<?= $row['product_image'] ?>"/>
                <a href="#"><span class="texto"></span></a>
            </div>
            </a>
            <h3 class="text-center"><?= $row['product_name'] ?></h3>
            <div class="text-center text-black rounded p-1" style="font-size: 20px;">Valor&nbsp;$<?= $row['product_price'] ?></div>
            <button class="btn btn-success btn-block" type="button" value="Add to cart" onclick="cart.add(<?= $row['product_id'] ?>);">Adicionar ao carrinho</button>
          </div>
          </div>
          </div>
        <?php }
      } else {
        echo "No products found.";
      }
      ?>

product php.

   <?php
$products = $_GET["product_id"];
  if (is_array($products)) {
    foreach ($products as $id => $row) { }};
    $row['product_name']
?>

  • where is the form with method get?

  • I did as follows, but I was not successful, even goes to page product.php with the id, but the page is all blank................ <form method="GET"action="product.php"> <a href="product.php?= <?= $Row['product_id'] ? >"> <div class="Hover-image"> <img class="card-img-top border-Secondary" src="images/<?= $Row['product_image'] ? >"/> <a href="#"><span class="text"></span></a> </div> </a> </form>

  • already gave a print_r("<pre>"); print_r($_GET);Exit;

2 answers

0

Hello

You are putting in $_GET["product_id"], while in URI you are putting only 'id'.

You need to change one of the two

<a href="product.php?product_id=<?= $row['product_id'] ?>">

putting so it should work.

  • the page even redirects to the link when I click, EXAMPLE: http://site.com/product.php?product_id=2 but everything turns white

0

I’ll leave all the index.php code to see if it helps.

<?php
/* [INIT - GET PRODUCTS] */
require __DIR__ . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "2a-config.php";
require PATH_LIB . "2b-lib-db.php";
require PATH_LIB . "3a-lib-products.php";
$pdtLib = new Products();
$products = $pdtLib->get();

/* [HTML] */ ?>
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="public/style.css">
    <script src="public/4a-cart.js"></script>
    <script src="public/slider.js"></script>
  </head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <body>

  <?php
     include ("header.php");
     ?>

    <!-- [PRODUCTS] --> 
    <div id="page-products"><?php
      if (is_array($products)) {
        foreach ($products as $id => $row) { ?>
          <div class="col-sm-6 col-md-4 col-lg-3 mb-2">
             <div class="card-deck">
               <div class="card p-2 border-secondary mb-2">
        <a href="product.php?product_id=<?= $row['product_id'] ?>">
            <div class="hover-image">
                <img class="card-img-top border-secondary" src="images/<?= $row['product_image'] ?>"/>
                <a href="#"><span class="texto"></span></a>
            </div>
            </a>
            <h3 class="text-center"><?= $row['product_name'] ?></h3>
            <div class="text-center text-black rounded p-1" style="font-size: 20px;">Valor&nbsp;$<?= $row['product_price'] ?></div>
            <button class="btn btn-success btn-block" type="button" value="Add to cart" onclick="cart.add(<?= $row['product_id'] ?>);">Adicionar ao carrinho</button>
          </div>
          </div>
          </div>
        <?php }
      } else {
        echo "No products found.";
      }
      ?>

Browser other questions tagged

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