Jump to content

How to make a Dynamic Product Detail page?


mrbobey

Recommended Posts

I have a product page which populates all my products in one page. I have also a detail page which gives details on a product which i wanted to know. My problem is when I am going to click on the product that I want the detail page shows incorrect product details. I just want one detail product page so that it will be easy to edit the page in the future.

I am asking an Idea on how to make one detail page in all of my products.. thanks...

Link to comment
Share on other sites

Suppose your product page has several images

    +------------------+       +------------------+
    |                  |       |                  |
    |    Product A     |       |   Product B      |
    |                  |       |                  |
    |                  |       |                  |
    |                  |       |                  |
    | href =           |       | href =           |
    | detail.php?id=1  |       | detail.php?id=2  |
    |                  |       |                  |
    +------------------+       +------------------+

    
    +------------------+       +------------------+
    |                  |       |                  |
    |   Product C      |       |   Product D      |
    |                  |       |                  |
    |                  |       |                  |
    |                  |       |                  |
    | href =           |       | href =           |
    | detail.php?id=3  |       | detail.php?id=4  |
    |                  |       |                  |
    +------------------+       +------------------+

Apply a link from each image to the detail.php page passing the id of the product.

In detail.php,

  • get the id passed in the link
  • query your database tables for the product info using that id
  • display the product details
Link to comment
Share on other sites

I think i might doing that already... here is the code:

My Product Page

<?php

session_start();

require_once 'connection.php';

$sql = "SELECT * FROM featuredlist";

$stmt = mysqli_stmt_init($conn1);

if(mysqli_stmt_prepare($stmt, $sql)){

  mysqli_stmt_execute($stmt);

  $result = mysqli_stmt_get_result($stmt);

  while($row = mysqli_fetch_assoc($result)) {

    $Name = $row["ProductName"];

    $Price = $row["Price"];

    $Image = $row["ProductImage"];

    $Link = $row["ProductLink"];

    $_SESSION["ProdID"] = $row["ProductID"];  ---> applying session so that the ID of the product will be passed to the detail page

echo '<div class="col-4">

      <a href="Ariel-with-downy.html"><img src="'.$Image.'"></a>';

echo '<h4>'.$Name.'</h4>

      <p>'.$Price.'</p>

      </div>

      ';

  }

} 

else {

  echo "0 results";

}

mysqli_close($conn1);

?>

<?php

 

My Product Detail Page

 

        require_once '../connection/connection.php';

        $sql = "SELECT * FROM featuredlist WHERE ProductID = ?";

        $stmt = mysqli_stmt_init($conn1);

        if (!mysqli_stmt_prepare($stmt, $sql)) {

         echo 'connection error';

        }

        $value = $_SESSION["ProdID"]; -----> i am passing now the Product ID into the variable which is needed for the query.

        mysqli_stmt_bind_param($stmt, "i", $value);

        mysqli_stmt_execute($stmt);

        $resultdata = mysqli_stmt_get_result($stmt);

        if($row = mysqli_fetch_assoc($resultdata)){

            $DetailName = $row["ProductName"];

            $DetailPrice = $row["Price"];

            $DetailImage = $row["ProductImage"];

            $DetailCategory = $row["Category"];

            $DetailDetail = $row["ProductDetail"];           

     echo '<div class="col-2">

                <img src="'.$DetailImage.'" width="100%">

                </div>

                <div class="col-2">

                <h3>Category</h3>

                <p>'.$DetailCategory.'</p>

                <h1>'.$DetailName.'</h1>

                <h4>'.$DetailPrice.'php</h4>

                <form class="buy" action = "" method = "POST">

                <a href="../Connection/buy.php" name = "buybtn" class="buy-btn">Add to Cart</a>

                </form>

                <h3>Product Information</h3><br>

                <p>'.$DetailDetail.'</p>

                </div>';

            }

        ?>

 

My problem is when i click the product in the product page, it shows the last product on the detail page.

Edited by mrbobey
Link to comment
Share on other sites

you mean that there is no way to make this happen? instead for having a product detail page each product? there is no problem when using a link specified to that product because i already have that on database but there a too many products i have to make a product detail page so that they have a specified link.

Link to comment
Share on other sites

16 minutes ago, Barand said:

You appear to use $_SESSION['prod_id'] every time. When does that get changed? You should be using the id from the product's link each time.

 

PS USe code tags (<> button in toolbar)

I think it will change when it loops until no products available..

Link to comment
Share on other sites

Simple example

products.php   -   list products with links containing the product's' id

$res = $db->query("SELECT prod_id
                        , description
                   FROM featuredlist;     
                  ");
$output = '';
foreach ($res as $row) {
    $output .= <<<HTML
        <div class='product'>
            <a href="detail.php?id={$row['prod_id']}">
                {$row['description']}
            </a>
        </div>\n
HTML;
    
}

image.png.89fa4fa0daa828f874dcb685dba664c3.png

detail.php
 

$prod_id = $_GET['id'] ?? 0;                       // get product id from the query string

if ($prod_id != 0) {
    $stmt = $db->prepare("SELECT description 
                          FROM featuredlist
                          WHERE prod_id = ?
                          ");
    $stmt->execute([$prod_id]);
    $row = $stmt->fetch();
    
    // output product details here
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.