The second-hand recycling website uses the real-time update function of the shopping cart developed by PHP
With the improvement of environmental protection awareness in modern society, more and more people have begun to pay attention to the recycling of second-hand items. In order to facilitate people to trade second-hand recycled items, many second-hand recycling websites have emerged.
On second-hand recycling websites, the shopping cart function is an indispensable and important component. It helps users conveniently manage and track their selected second-hand items, and updates the number of items in the shopping cart in real time. This article will introduce how to use PHP to develop a shopping cart with real-time update function.
First, we need to create a table in the database to store shopping cart information. In this table, we can store fields such as user ID, product ID, product name, product quantity, product unit price, etc. Here is an example of how the table might be structured:
CREATE TABLE shopping_cart ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, product_id INT, product_name VARCHAR(255), quantity INT, price DECIMAL(10, 2) );
Next, we need to create a shopping cart page for users to select second-hand items. On this page we can list all recyclable items and provide an "Add to Cart" button for each item. When the user clicks the button, we can call a PHP script to add the selected item to the shopping cart.
Sample code:
<?php // 获取商品信息 $product_id = $_POST['product_id']; $product_name = $_POST['product_name']; $price = $_POST['price']; // 获取用户信息(这里假设用户已登录) $user_id = $_SESSION['user_id']; // 检查购物车中是否已经存在相同的商品 $query = "SELECT * FROM shopping_cart WHERE user_id = $user_id AND product_id = $product_id"; $result = mysqli_query($connection, $query); if (mysqli_num_rows($result) > 0) { // 如果已存在,则更新商品数量 $row = mysqli_fetch_assoc($result); $quantity = $row['quantity'] + 1; $update_query = "UPDATE shopping_cart SET quantity = $quantity WHERE user_id = $user_id AND product_id = $product_id"; mysqli_query($connection, $update_query); } else { // 如果不存在,则添加新的商品到购物车 $insert_query = "INSERT INTO shopping_cart (user_id, product_id, product_name, quantity, price) VALUES ($user_id, $product_id, '$product_name', 1, $price)"; mysqli_query($connection, $insert_query); } // 返回购物车页面 header('Location: shopping_cart.php'); ?>
In the shopping cart page, we can display the items in the shopping cart and provide the function of modifying the quantity. In order to achieve real-time update effect, we can use Ajax technology to achieve it.
Sample code:
// 监听数量输入框的变化 $('.quantity-input').change(function() { var product_id = $(this).data('product-id'); var new_quantity = $(this).val(); // 向服务器发送Ajax请求,更新购物车中商品的数量 $.ajax({ url: 'update_quantity.php', type: 'POST', data: { product_id: product_id, new_quantity: new_quantity }, success: function(response) { // 更新购物车显示 $('.product-quantity[data-product-id="' + product_id + '"]').text(new_quantity); calculateTotalPrice(); } }); }); // 计算总价 function calculateTotalPrice() { var total_price = 0; $('.product-row').each(function() { var price = $(this).find('.product-price').text(); var quantity = $(this).find('.product-quantity').text(); total_price += parseFloat(price) * parseInt(quantity); }); $('.total-price').text(total_price); }
On the server side, we need to write a PHP script that updates the number of items in the shopping cart.
Sample code:
<?php // 获取商品和新数量 $product_id = $_POST['product_id']; $new_quantity = $_POST['new_quantity']; // 获取用户ID(这里假设用户已登录) $user_id = $_SESSION['user_id']; // 更新购物车中商品的数量 $update_query = "UPDATE shopping_cart SET quantity = $new_quantity WHERE user_id = $user_id AND product_id = $product_id"; mysqli_query($connection, $update_query); ?>
Through the above sample code, we can implement a shopping cart with real-time update function. Users can easily add, delete and update items in the shopping cart, making it easier to trade second-hand items.
To sum up, the second-hand recycling website uses the shopping cart real-time update function developed in PHP to provide users with an efficient and convenient experience. The shopping cart function allows users to easily manage and track selected second-hand items, and update the item quantity in real time. Through reasonable use of PHP and Ajax technology, we can easily achieve this function.
The above is the detailed content of The second-hand recycling website uses the shopping cart real-time update function developed in PHP. For more information, please follow other related articles on the PHP Chinese website!