Home > Article > Backend Development > Build a PHP mall: implement product search and sorting functions
Building a PHP mall: realizing product search and sorting functions
With the vigorous development of the e-commerce industry, building a powerful PHP mall has become one of the pursuits of web developers. Among them, product search and sorting functions are one of the indispensable core functions of a successful e-commerce platform. This article will introduce how to use PHP to implement product search and sorting functions, and provide relevant code examples.
1. Implementation of product search function
The product search function is one of the main ways for users to find specific products in the mall. Below we will introduce how to use PHP to implement the product search function.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否正常 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?>
<form action="search.php" method="get"> <input type="text" name="keyword" placeholder="请输入关键字"> <input type="submit" value="搜索"> </form>
<?php if(isset($_GET['keyword'])) { $keyword = $_GET['keyword']; $sql = "SELECT * FROM products WHERE name LIKE '%$keyword%'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "商品名称: " . $row['name']. " - 价格: " . $row['price']. "<br>"; } } else { echo "没有找到相关商品"; } } ?>
2. Implementation of the product sorting function
The product sorting function allows users to sort products according to certain rules for more convenient browsing and Select an item. Below we will introduce how to use PHP to implement the product sorting function.
<a href="sort.php?sortby=price">按价格排序</a> <a href="sort.php?sortby=name">按名称排序</a>
<?php if(isset($_GET['sortby'])) { $sortby = $_GET['sortby']; if($sortby == 'price') { $sql = "SELECT * FROM products ORDER BY price ASC"; } elseif($sortby == 'name') { $sql = "SELECT * FROM products ORDER BY name ASC"; } $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "商品名称: " . $row['name']. " - 价格: " . $row['price']. "<br>"; } } else { echo "没有找到相关商品"; } } ?>
Summary:
Through the above code examples, we can see how to use PHP to implement product search and sorting functions. Integrating these functions into your PHP mall can improve the user experience and make it easier for users to find and select the products they need.
Of course, we only provide a simple implementation method, and you can make appropriate modifications and extensions according to your own needs to meet the specific requirements of the mall. I hope this article can inspire you to build product search and sorting functions in your PHP mall.
The above is the detailed content of Build a PHP mall: implement product search and sorting functions. For more information, please follow other related articles on the PHP Chinese website!