Home >Backend Development >PHP Tutorial >PHP development technology for second-hand recycling websites provides users with convenient search functions
The PHP development technology of second-hand recycling website provides users with convenient search functions
With the progress of society and the limited resources, second-hand recycling has become one of the important ways for people to save resources. In order to facilitate users to quickly find second-hand items that meet their needs, developers of second-hand recycling websites have developed convenient search functions using PHP technology.
The search function is one of the core functions of second-hand recycling websites. It helps users quickly find the second-hand items they need by entering keywords. Below we will introduce how to use PHP development technology to achieve this function.
First, we need to create a database to store the second-hand item information uploaded by users. You can use a database management system such as MySQL to create a database named "second_hand". Create a table named "items" in the database to store item-related information, such as item name, price, description, etc. The structure of the table can be as follows:
CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `price` decimal(10,2) NOT NULL, `description` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the search page of the website, add a search box and a search button for entering key words and trigger a search operation. Page design can be done using HTML and CSS technologies.
<form action="search.php" method="GET"> <input type="text" name="keyword" placeholder="请输入关键词"> <input type="submit" value="搜索"> </form>
Next, we need to write the search.php file to process the keywords entered by the user and return matching results.
First, connect to the database:
$host = "localhost"; $username = "root"; $password = "123456"; $dbname = "second_hand"; $conn = new mysqli($host, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); }
Then, get the keywords entered by the user and construct the query statement:
$keyword = $_GET['keyword']; $sql = "SELECT * FROM items WHERE name LIKE '%{$keyword}%'"; $result = $conn->query($sql);
Finally, traverse the query results and output them to the page:
if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "物品名称: " . $row["name"]. " - 价格: " . $row["price"]. " - 描述: " . $row["description"]. "<br>"; } } else { echo "没有找到相关物品"; } $conn->close();
Through the above code example, we can implement a simple search function. After the user enters the keyword, the system retrieves matching results from the database and displays them on the page.
Through PHP development technology, we can easily implement the search function of second-hand recycling websites. Users can quickly find qualified second-hand items according to their needs, thereby achieving effective use of resources and environmental protection. Writing efficient search algorithms and good interface design will further enhance user experience and website competitiveness.
The above is the detailed content of PHP development technology for second-hand recycling websites provides users with convenient search functions. For more information, please follow other related articles on the PHP Chinese website!