Home  >  Article  >  Backend Development  >  Build a PHP mall: implement product search and sorting functions

Build a PHP mall: implement product search and sorting functions

WBOY
WBOYOriginal
2023-07-28 23:05:091476browse

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.

  1. Create database connection
    First, we need to create a database connection to store product information. You can use the following code to create a database connection:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否正常
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
?>
  1. Create a product search page
    Next, we need to create a product search page in the mall website so that users can enter keywords Search for products. A simple search form can be created using the following code:
<form action="search.php" method="get">
    <input type="text" name="keyword" placeholder="请输入关键字">
    <input type="submit" value="搜索">
</form>
  1. Processing search requests
    When the user submits the search form, we need to process the search request and display the corresponding search results. You can use the following code to process search requests:
<?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.

  1. Create sorting links
    First, we need to create some sorting links in the mall website so that users can click on them to sort products according to different rules. You can use the following code to create some simple sorting links:
<a href="sort.php?sortby=price">按价格排序</a>
<a href="sort.php?sortby=name">按名称排序</a>
  1. Handling sorting requests
    When the user clicks the sorting link, we need to process the corresponding sorting request and display the sorting results. The following code can be used to handle sorting requests:
<?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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn