我有一个网站,将出售多个产品。到目前为止,我的列表页面看起来像这样
<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从DB中拉出并显示需要在屏幕上显示的内容。但是我的问题是单击产品后,新页面将如何知道已通过了哪些产品,我能想到的唯一方法是创建所有单个产品页面。
您需要定义这样的路由
my-website.com/product.php?id=2
id
将是每种产品的ID。
或
如果您的表上有slug
列,则可以做
my-website.com/product.php?slug=a-nice-product
slug
将是为每种产品生成的存储的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注入的更多信息
再次,一个清洁的URL看起来像
my-website.com/product/2
或
my-website.com/product/a-nice-product
,为此,您需要设置一些HTACCESS规则。