How do I change the page title to match the product title dynamically using PHP and MySQL?

54 Views Asked by At

I'm building an e-commerce website and I want to display the product name in the page title when I go to the product details page details.php.

I have the $title variable in <title> tag on head.php page and I'm calling this variable at top of all of my pages like this:

<?php
      
$title = "TBROS | Product details ";

include ('partials/head.php')
?>

I'm getting the data from the products table (product_tbl) and displaying it in the product card including the product title.

So how can I get this product title in the page title?.

  <div class="products-container">
            <?php
                     $sql = "SELECT * FROM product_tbl";
                     $result = mysqli_query($con, $sql);
            
                    if ($result) {
                        $count = mysqli_num_rows($result);
                        if ($count>0) {
                            while ($row=mysqli_fetch_assoc($result)) {
                                $id=$row['id'];
                                $image_name = $row['image_name'];
                                $pro_title =$row['title'];
                                ?>
            <div class="card">
                <?php
                if ($image_name=="") {
                    echo "<p style='color: red;'>Image not Added.</p>";

                }else{
                    ?>
                <img src="<?php echo WEBSITEURL;?>images/products/<?php echo $image_name;?>">
                <?php
                }
                ?>
                <div class="card-body">
                    <h4 class="card-title"><?php echo $pro_title?></h4>
                    <span class="price">$<?php echo $row['price']?></span>
                    <a href="details.php?id=<?php echo $id?>" class="cart-btn">View
                        Details</a>
                </div>
            </div>
            <?php
                    }
                }
            }
            ?>


        </div>
0

There are 0 best solutions below