The second-hand recycling website uses the product brand screening function developed by PHP
With the development of society and the upgrading of consumption, the rise of the second-hand market has attracted more and more attention. Many people buy and sell their unwanted items through second-hand recycling websites to achieve the purpose of resource reuse and environmental protection. In order to provide a better user experience, some second-hand recycling websites have begun to use PHP to develop some specific functions, such as product brand filtering functions. This article will use a simple code example to introduce how a second-hand recycling website uses PHP to develop a product brand filtering function.
Before developing the product brand filtering function, you first need to prepare a database table for the product list, which contains some basic attributes of the product, such as product name, price, brand, etc. We can use MySQL to create a database table named products and add some sample data, as shown below:
CREATE TABLE products ( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL, brand VARCHAR(50) NOT NULL ); INSERT INTO products (name, price, brand) VALUES ('iPhone 11', 6999, 'Apple'), ('Galaxy S10', 4999, 'Samsung'), ('Mate 30', 5999, 'Huawei'), ('Mi 9', 2999, 'Xiaomi'), ('P30', 3999, 'Huawei');
Next, we start writing PHP code to implement the product brand filtering function. First, you need to connect to the MySQL database and query the database based on the brand selected by the user to obtain a list of eligible products. Here is a simple code example:
<?php $host = 'localhost'; $db = 'your_database_name'; $user = 'your_username'; $pass = 'your_password'; $conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass); // 获取用户选择的品牌 $brand = isset($_GET['brand']) ? $_GET['brand'] : ''; // 根据品牌查询商品列表 $sql = "SELECT * FROM products"; if (!empty($brand)) { $sql .= " WHERE brand = :brand"; } $stmt = $conn->prepare($sql); if (!empty($brand)) { $stmt->bindParam(':brand', $brand, PDO::PARAM_STR); } $stmt->execute(); $products = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>商品列表</title> </head> <body> <h1>商品列表</h1> <form action="" method="GET"> <select name="brand"> <option value="">全部品牌</option> <option value="Apple">Apple</option> <option value="Samsung">Samsung</option> <option value="Huawei">Huawei</option> <option value="Xiaomi">Xiaomi</option> </select> <button type="submit">筛选</button> </form> <ul> <?php foreach ($products as $product): ?> <li><?php echo $product['name']; ?></li> <?php endforeach; ?> </ul> </body> </html>
In the above code, we have used PDO to connect to the MySQL database and used prepared statements to prevent SQL injection attacks. Users can filter by selecting different brands through the form, and then based on the user's selection, the database will return a list of products that meet the conditions and display them on the page.
Through the above code examples, we briefly introduce how second-hand recycling websites use PHP to develop product brand filtering functions. Of course, actual website development requires more details and functions, such as paging, sorting, etc., but I hope this example can provide some reference and inspiration for readers. It is hoped that future second-hand recycling websites can provide users with better experiences and services through continuous innovation and improvement.
The above is the detailed content of Second-hand recycling website uses product brand filtering function developed in PHP. For more information, please follow other related articles on the PHP Chinese website!