My favorites function developed by second-hand recycling websites using PHP
In second-hand recycling websites, users are often interested in some specific products or transaction information, hoping to easily collect them and view them at any time. In order to meet the needs of users, we developed a convenient favorites function using PHP.
First, we need to create a favorites database table to store the user's favorites information. The table mainly contains the following fields: collection ID, user ID, product ID, and collection time. The collection ID is the primary key, and the user ID and product ID are associated with other related tables.
Next, we use PHP to develop the user interface to implement the functions of adding favorites, viewing favorites, and canceling favorites.
The first is the function of adding favorites. When users browse the website, they can click the "Favorite" button next to a product or transaction information to add it to their favorites. We can insert the following code into the corresponding page:
<?php // 获取当前用户ID和商品ID $userID = $_SESSION['userID']; $itemID = $_GET['itemID']; // 根据用户ID和商品ID检查是否已经收藏过 $query = "SELECT * FROM favorites WHERE userID = $userID AND itemID = $itemID"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { echo "该商品已经在收藏夹中"; } else { // 插入新的收藏记录 $query = "INSERT INTO favorites (userID, itemID, dateAdded) VALUES ($userID, $itemID, NOW())"; mysqli_query($conn, $query); echo "已成功添加至收藏夹"; } ?>
The next step is to view the collection function. Users can click the "My Favorites" link on the page to enter the favorites page and display all the products or transaction information that the user has collected. The following is a code example to implement this function:
<?php // 获取当前用户ID $userID = $_SESSION['userID']; // 获取用户收藏夹中的商品ID列表 $query = "SELECT itemID FROM favorites WHERE userID = $userID"; $result = mysqli_query($conn, $query); // 遍历商品ID列表,查询并显示相应的商品信息 while ($row = mysqli_fetch_assoc($result)) { $itemID = $row['itemID']; // 查询商品信息 $query = "SELECT * FROM items WHERE itemID = $itemID"; $itemResult = mysqli_query($conn, $query); $item = mysqli_fetch_assoc($itemResult); // 显示商品信息 echo "商品名称:" . $item['itemName'] . "<br>"; echo "商品价格:" . $item['itemPrice'] . "<br><br>"; } ?>
The last is the function of canceling collection. In the favorites page, users can click the "Cancel Favorites" button next to each item to remove the item from the favorites. The following is the corresponding code example:
<?php // 获取当前用户ID和要取消收藏的商品ID $userID = $_SESSION['userID']; $itemID = $_GET['itemID']; // 删除收藏记录 $query = "DELETE FROM favorites WHERE userID = $userID AND itemID = $itemID"; mysqli_query($conn, $query); echo "已成功取消收藏"; ?>
Through the above code example, we have implemented a PHP-based favorites function. When browsing the website, users can easily add products or transaction information of interest to their favorites, and view or cancel favorites at any time. This function not only improves the user experience, but also increases the user's stickiness to the website, bringing huge value to the development of second-hand recycling websites.
The above is the detailed content of The second-hand recycling website uses the My Favorites function developed in PHP. For more information, please follow other related articles on the PHP Chinese website!