Home >Backend Development >PHP Tutorial >Second-hand recycling website developed using PHP supports automatic matching of keywords
The second-hand recycling website developed using PHP supports automatic keyword matching
With the improvement of environmental awareness and the gradual scarcity of resources, the recycling and reuse of second-hand items has attracted more and more attention. The emergence of second-hand recycling websites provides a convenient platform for people to find items that need to be recycled or to hand over their idle items to professional recycling agencies for processing. In order to improve user experience and search accuracy, we will use PHP to develop a second-hand recycling website that supports automatic keyword matching.
Before we begin, let’s briefly introduce PHP. PHP is a server-side scripting language especially suitable for website development. It can be embedded into HTML, allowing developers to write dynamic content directly in web pages. Second-hand recycling websites developed using PHP can realize functions such as interaction with users, data processing and display.
First, we need to create a homepage so that users can enter keywords to search. The homepage can be laid out and beautified using HTML and CSS, and user search requests can be processed through PHP. On the homepage, we can set up a text input box and a search button. Users can enter keywords in the text input box and then click the search button to search. The HTML code is as follows:
<!DOCTYPE html> <html> <head> <title>二手回收网站</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <h1>二手回收网站</h1> <form action="search.php" method="GET"> <input type="text" name="keyword" placeholder="请输入关键词"> <input type="submit" value="搜索"> </form> </div> </body> </html>
In the above code, we set up a search form through the ff9c23ada1bcecdd1a0fb5d5a0f18437
element, and its action
attribute points to the search processing PHP file search.php
, use the GET
method to submit the form data. The keywords entered by the user will be passed to search.php
for processing through name="keyword"
.
Next, let’s take a look at the code example of the search.php
file. In this file, we will get the keywords entered by the user and match them with the item information saved in the database. Successfully matched items will be displayed to the user in the form of a list. The PHP code is as follows:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "second_hand"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } // 获取用户输入的关键词 $keyword = $_GET["keyword"]; // 根据关键词从数据库中查询匹配的物品信息 $sql = "SELECT * FROM items WHERE item_name LIKE '%$keyword%'"; $result = $conn->query($sql); // 列表展示匹配的物品信息 if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<p>{$row['item_name']} - {$row['item_price']}</p>"; } } else { echo "没有找到匹配的物品"; } $conn->close(); ?>
In the above code, we first connect to a database named second_hand
. Then, obtain the keywords entered by the user through the $_GET
super global variable, and conduct a fuzzy matching query between the keywords and the items
table in the database. Finally, use the while
loop to display the matching item information to the user in list form.
To sum up, the second-hand recycling website we developed using PHP supports automatic matching of keywords and has good user experience and search accuracy. Users can easily enter keywords to search, and the system will automatically match the corresponding item information and display it to the user. In this way, users can easily find the second-hand items they need, achieving effective use of resources and sustainable development of the environment.
The above is the detailed content of Second-hand recycling website developed using PHP supports automatic matching of keywords. For more information, please follow other related articles on the PHP Chinese website!