Home >Backend Development >PHP Tutorial >How to use PHP to implement product multi-specification SKU function
How to use PHP to implement product multi-specification SKU function
Multi-specification product SKU is a common product attribute setting on e-commerce platforms, which allows merchants to set the same product Different specifications and prices to meet consumers' choices for different needs and preferences. This article will introduce how to use PHP to implement the multi-specification SKU function of products and provide corresponding code examples. Hope it helps readers in need.
1. Data structure design
Before implementing the multi-specification SKU function of products, we need to design the data structure of the database first. Suppose the product has three specifications: color, size and price. First, we need to create two database tables, one to store specification attribute information and one to store SKU information.
CREATE TABLE attributes
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(255) NOT NULL,
PRIMARY KEY (id
)
);
CREATE TABLE skus
(
id
int(11) NOT NULL AUTO_INCREMENT,
attribute_id
int(11) NOT NULL,
value
varchar(255) NOT NULL,
price
decimal(10,2) NOT NULL,
PRIMARY KEY (id
),
FOREIGN KEY (attribute_id
)
REFERENCES attributes
(id
)
ON DELETE CASCADE
);
2. Backend management page
First, we need to display the specification attribute list on the backend management page for Merchants add and edit specification attributes.
//Connect to the database
$conn = new mysqli("localhost", "username", "password", "database");
/ / Query specification attribute list
$query = "SELECT * FROM attributes";
$result = $conn->query($query);
?>
Specification attribute name |
---|
When merchants need to add or edit specification attributes, we provide corresponding forms for them to fill in and save to the database.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Process form submission
$name = $_POST['name' ];
//Save specification attributes to the database
$query = "INSERT INTO attributes (name
) VALUES ('{$name}')";
$conn ->query($query);
}
?>
In the background management page, we need to display SKU information list for merchants to add and edit SKU information.
// Query SKU information list
$query = "SELECT skus.*, attributes.name as attribute FROM skus JOIN attributes ON skus.attribute_id = attributes.id";
$result = $conn->query($query);
?>
Specification attributes | Specification Value | price |
---|---|---|
When merchants need to add or edit SKU information, we provide corresponding forms for them to fill in and save to the database.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Process form submission
$attributeId = $_POST['attribute_id' ];
$value = $_POST['value'];
$price = $_POST['price'];
// Save SKU information to the database
$query = " INSERT INTO skus (attribute_id
, value
, price
) VALUES ('{$attributeId}', '{$value}', '{$price}' )";
$conn->query($query);
}
?>
< ;form method="POST" action="">
<option value="" disabled selected>请选择规格属性</option> <?php // 查询规格属性列表 $query = "SELECT * FROM attributes"; $result = $conn->query($query); while ($row = $result->fetch_assoc()): ?> <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option> <?php endwhile; ?>
< ;label for="price">Price
3. Front-end product details page
On the front-end product details page, we need to display the specification attributes and SKU information of the product, and provide selection specification attributes. and corresponding SKU information functions.
// Query specification attribute list
$query = "SELECT * FROM attributes";
$result = $conn->query($query);
?>
<script><br>$(document).ready(function() {<br> // Specification attribute selection Listening to events<br> $('#attribute').change(function() {</script>
var attributeId = $(this).val(); $.ajax({ url: 'get_sku_info.php', method: 'POST', dataType: 'json', data: {attributeId: attributeId}, success: function(data) { // 清空SKU信息显示区域 $('#sku-info').empty(); // 根据返回的SKU信息动态添加到显示区域 $.each(data, function(index, item) { var skuHtml = '<div>'; skuHtml += '<span>' + item.attribute + ': ' + item.value + '</span>'; skuHtml += '<span>价格: ' + item.price + '</span>'; skuHtml += '</div>'; $('#sku-info').append(skuHtml); }); } });
});
});
On the front page, we need to use Ajax to request the backend interface to obtain the SKU information under the corresponding specification attribute.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$attributeId = $_POST['attributeId'];
// Query the SKU information under the corresponding specification attributes
$query = "SELECT skus.*, attributes.name as attribute FROM skus JOIN attributes ON skus.attribute_id = attributes.id WHERE skus.attribute_id = '{$attributeId}' ";
$result = $conn->query($query);
$skus = [];
while ($row = $result->fetch_assoc()) {
$skus[] = $row;
}
echo json_encode($skus);
}
?>
IV. Summary
This article introduces how to use PHP implements the SKU function of multiple specifications of products. By designing the data structure of the database and developing the back-end management page and front-end product details page, we can allow merchants to flexibly add and edit specification attributes and SKU information, and allow consumers to select the corresponding SKU based on the specification attributes. I hope this article will inspire and help readers who are developing e-commerce platforms.
The above is the detailed content of How to use PHP to implement product multi-specification SKU function. For more information, please follow other related articles on the PHP Chinese website!