mrbobey Posted July 25, 2021 Share Posted July 25, 2021 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... Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/ Share on other sites More sharing options...
Barand Posted July 25, 2021 Share Posted July 25, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588577 Share on other sites More sharing options...
mrbobey Posted July 25, 2021 Author Share Posted July 25, 2021 (edited) 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 July 25, 2021 by mrbobey Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588578 Share on other sites More sharing options...
Barand Posted July 25, 2021 Share Posted July 25, 2021 (edited) 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) Edited July 25, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588579 Share on other sites More sharing options...
mrbobey Posted July 25, 2021 Author Share Posted July 25, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588580 Share on other sites More sharing options...
mrbobey Posted July 25, 2021 Author Share Posted July 25, 2021 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.. Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588581 Share on other sites More sharing options...
Barand Posted July 25, 2021 Share Posted July 25, 2021 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; } 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 }  Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588582 Share on other sites More sharing options...
mrbobey Posted July 25, 2021 Author Share Posted July 25, 2021 thank you sir i have an idea now, sounds that you are passing the product ID together with the url thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588583 Share on other sites More sharing options...
Barand Posted July 25, 2021 Share Posted July 25, 2021 13 minutes ago, mrbobey said: sounds that you are passing the product ID together with the url thank you very much Yes - just like those sample links in my first reply Quote Link to comment https://forums.phpfreaks.com/topic/313429-how-to-make-a-dynamic-product-detail-page/#findComment-1588585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.