我有一个可以销售多种产品的网站。到目前为止,我的列表页面看起来像这样
<div class="col-sm-9 padding-right">
<div class="features_items"><!--features_items-->
<h2 class="title text-center">My Products</h2>
<?php
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql2 = "SELECT * FROM myproducts WHERE 1 ORDER BY ProductName ASC";
$myresult = $conn->query($sql2);
$x = 1;
while ($row = $myresult->fetch_assoc()) {
?>
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="../../images/myImages/<?php echo $row['ProductImage1'];?>_0001_1.jpg" alt="../../images/myImages/<?php echo $row['ProductImage1'];?>_0001_1.jpg" />
<h2><?php echo $row['ProductPrice']; ?></h2>
<p><?php echo $row['ProductName']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2><?php echo $row['Vendor']; ?></h2>
<h2><?php echo $row['ProductName']; ?></h2>
<p>$ <?php echo $row['ProductPrice']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
</div>
<?php
}
$conn->close();
?>
上面的代码循环遍历数据库中的所有内容并在屏幕上显示它们。我想要的是当你点击产品时,它会转到另一个页面并显示特定的产品信息。我有数百种产品可以单独显示,我想知道是否有一种方法我不必编写100多个html文件,只是这样每个页面都可以单独显示。
理想情况下,设置将类似于上面的内容,它使用php从数据库中提取并显示它需要在屏幕上显示的内容。但我的问题是,一旦产品被点击,新页面将如何知道产品已经通过,我能想到的唯一方法就是创建所有单独的产品页面。
答案 0 :(得分:0)
你需要定义这样的路线
my-website.com/product.php?id=2
其中id
将是每个产品的ID。
或
如果您的桌子上有slug
列,则可以
my-website.com/product.php?slug=a-nice-product
slug
将是为每个产品生成的存储段塞。
因此,无论哪种方式适用于您,您都可以使用该产品的id
或slug
来获取特定产品,以便在产品详细信息页面上显示。
显示格式将基于预定义的模板。 类似于您对产品列表所做的事情将是
<?php
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['id']; // in the case of "my-website.com/product.php?id=2"
$sql2 = "SELECT * FROM myproducts WHERE id=$id ORDER BY ProductName ASC"; // NOTE:: This is a bad practice
$myresult = $conn->query($sql2);
if($myresult):
?>
<div class="col-sm-9 padding-right">
<h2 class="title text-center">Details about <?= $myresult['ProductName']; ?></h2>
<!-- LIST PRODUCT DETAILS BASED ON TEMPLATE DESIGN -->
</div>
<?php
endif;
$conn->close();
?>
<强>更新强>
$id = $_GET['id']; // in the case of "my-website.com/product.php?id=2"
$sql2 = "SELECT * FROM myproducts WHERE id=$id ORDER BY ProductName ASC";
// NOTE:: This is a bad practice as it is open to SQL injection attacks.
详细了解SQL injection
同样,一个更干净的网址看起来像这样
my-website.com/product/2
或
my-website.com/product/a-nice-product
但要实现这一目标,您需要设置一些htaccess规则。