Home > Article > Backend Development > How to build a product multi-specification SKU system in PHP
How to build a product multi-specification SKU system in PHP
The product multi-specification SKU system is a very common product management method in e-commerce. Through this system Multiple specifications and attributes of goods can be easily managed. When building a product multi-specification SKU system in PHP, you can use a database to store product specifications and attribute data, and use PHP's array and loop structures to process and display these data.
Below I will introduce in detail how to build a simple product multi-specification SKU system in PHP.
First, we need to create two tables in the database, one to store product specifications and the other to store product attributes.
The structure of the specification table is as follows:
CREATE TABLE `specifications` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The structure of the attribute table is as follows:
CREATE TABLE `attributes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `specification_id` int(11) NOT NULL, PRIMARY KEY (`id`), INDEX `specification_id` (`specification_id`), CONSTRAINT `attributes_ibfk_1` FOREIGN KEY (`specification_id`) REFERENCES `specifications` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then, we can obtain the specifications and attribute data of the product through the following code:
// 获取规格 $specs = []; $sql = "SELECT * FROM specifications"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $specs[$row['id']] = $row['name']; } } // 获取属性 $attrs = []; foreach ($specs as $spec_id => $spec_name) { $sql = "SELECT * FROM attributes WHERE specification_id = " . $spec_id; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $attrs[$spec_name][$row['id']] = $row['name']; } } }
Next, we can use the form to display the specifications and attributes of the product, and select different specifications and attributes of the product. The following is a simple form example:
<form action="" method="post"> <?php foreach ($specs as $spec_id => $spec_name): ?> <label><?php echo $spec_name; ?>:</label> <select name="spec_<?php echo $spec_id; ?>"> <option value="">请选择</option> <?php foreach ($attrs[$spec_name] as $attr_id => $attr_name): ?> <option value="<?php echo $attr_id; ?>"><?php echo $attr_name; ?></option> <?php endforeach; ?> </select> <?php endforeach; ?> <button type="submit">加入购物车</button> </form>
Finally, we can obtain the product specifications and attributes selected by the user through the following code:
$sku = []; foreach ($_POST as $key => $value) { if (strpos($key, 'spec_') === 0 && $value != "") { $spec_id = substr($key, strlen('spec_')); $spec_name = $specs[$spec_id]; $attr_id = $value; $attr_name = $attrs[$spec_name][$attr_id]; $sku[$spec_name] = $attr_name; } }
Through the above steps, we can use PHP Build a simple product multi-specification SKU system. Through this system, we can easily manage multiple specifications and attributes of goods, and realize the selection and display of these specifications and attributes.
Of course, the actual product multi-specification SKU system will be more complex, and more functions and details need to be considered. However, through the above examples, you can learn how to build a simple product multi-specification SKU system based on PHP, and expand and optimize it on this basis. Hope this article is helpful to you!
The above is the detailed content of How to build a product multi-specification SKU system in PHP. For more information, please follow other related articles on the PHP Chinese website!