Home > Article > Backend Development > Second-hand recycling website developed using PHP supports popular tag navigation
The second-hand recycling website developed using PHP supports popular tag navigation
With the prosperity of second-hand transactions, second-hand recycling websites have become an indispensable part of people's daily lives. In order to improve the user experience and make it easier for users to find the items they are interested in, we need to add a function that supports popular tag navigation to the website. This article will use PHP as an example to introduce how to implement this function.
1. Create database and data table
First, we need to create a database and data table to store tag information. Open the MySQL management tool, create a database named "recycle", and create a data table named "tags" in the database. The structure of the data table is as follows:
CREATE TABLE `tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. Add tag data
Add some tag data to the database for subsequent use. You can add some sample data by executing the following SQL statement:
INSERT INTO `tags` (`name`) VALUES ('手机'); INSERT INTO `tags` (`name`) VALUES ('电脑'); INSERT INTO `tags` (`name`) VALUES ('服装'); INSERT INTO `tags` (`name`) VALUES ('家居'); INSERT INTO `tags` (`name`) VALUES ('书籍');
3. Page design
Next, we need to design a page to display popular tag navigation. The following is a simple example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>二手回收网站</title> </head> <body> <h1>热门标签</h1> <ul> <?php // 连接数据库 $conn = new mysqli("localhost", "root", "password", "recycle"); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询标签数据 $sql = "SELECT * FROM tags"; $result = $conn->query($sql); // 输出标签列表 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<li><a href='search.php?tag=".$row["name"]."'>".$row["name"]."</a></li>"; } } else { echo "暂无标签"; } // 关闭数据库连接 $conn->close(); ?> </ul> </body> </html>
4. Search page
In the tag navigation page, we add a link to each tag pointing to a search page, which is retrieved from the URL Get the tag parameter and use this parameter to search for items. The following is a simple example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>搜索结果</title> </head> <body> <?php // 获取标签参数 $tag = $_GET["tag"]; // 连接数据库 $conn = new mysqli("localhost", "root", "password", "recycle"); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询匹配标签的物品 $sql = "SELECT * FROM items WHERE tags LIKE '%".$tag."%'"; $result = $conn->query($sql); // 输出搜索结果 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "名称:" . $row["name"] . "<br>"; echo "价格:" . $row["price"] . "<br>"; echo "描述:" . $row["description"] . "<br><br>"; } } else { echo "未找到相关物品"; } // 关闭数据库连接 $conn->close(); ?> </body> </html>
The above is the implementation process of supporting popular tag navigation for a second-hand recycling website developed using PHP. By adding the popular tag navigation function, users can quickly find the items they are interested in, which improves the usability and user experience of the website. Of course, the above is just a simple example, you can expand and improve it according to your actual needs.
The above is the detailed content of Second-hand recycling website developed using PHP supports popular tag navigation. For more information, please follow other related articles on the PHP Chinese website!