Home  >  Article  >  Backend Development  >  PHP development: How to implement product search function

PHP development: How to implement product search function

PHPz
PHPzOriginal
2023-09-20 11:39:23929browse

PHP development: How to implement product search function

PHP development: How to implement product search function

The product search function is very common in e-commerce websites. It provides users with a convenient product query and browsing experience. In this article, we will explore how to use PHP to implement a simple product search function and provide specific code examples.

1. Database preparation
First, we need to prepare a database suitable for product search. The database should contain product-related information, such as product name, price, description, etc. We can use relational databases such as MySQL to store and manage this data.

Create a table named "products", containing the following columns:

  • id: product ID, set as primary key
  • name: product name
  • price: Product price
  • description: Product description

2. Front-end page design
We will use simple HTML and CSS to create a search page. In the search page, users can enter keywords to search.

<!DOCTYPE html>
<html>
<head>
    <title>商品搜索</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <h1>商品搜索</h1>
    <form action="search.php" method="GET">
        <input type="text" name="keyword" placeholder="请输入关键词">
        <input type="submit" value="搜索">
    </form>
</body>
</html>

3. PHP code implementation
In the search.php file, we will receive the keywords passed from the search page and obtain the corresponding product list by querying the database.

<?php
// 连接到数据库
$connect = mysqli_connect("localhost", "username", "password", "database");

// 检查连接是否成功
if(mysqli_connect_error()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 获取关键词
$keyword = $_GET["keyword"];

// 查询数据库中的商品
$query = "SELECT * FROM products WHERE name LIKE '%$keyword%' OR description LIKE '%$keyword%'";
$result = mysqli_query($connect, $query);

if(mysqli_num_rows($result) > 0) {
    // 显示查询结果
    while($row = mysqli_fetch_assoc($result)) {
        echo "<h2>".$row["name"]."</h2>";
        echo "<p>".$row["description"]."</p>";
        echo "<p>价格:".$row["price"]."</p>";
        echo "<hr>";
    }
} else {
    echo "未找到相关商品。";
}

// 关闭连接
mysqli_close($connect);
?>

At this point, we have completed a simple product search function. Users can query products by entering keywords on the search page, and the PHP code will query the products in the database based on the keywords and display them on the page.

It should be noted that the above code is just an example. In actual development, more functions may be needed, such as paging, sorting, etc. In addition, in order to improve search efficiency, you can consider establishing a full-text index of product names and descriptions, and use the full-text search function to replace simple fuzzy queries.

I hope this article can help you understand how to use PHP to implement product search functions, and provides specific code examples. Happy development!

The above is the detailed content of PHP development: How to implement product search function. 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