Hi Team
I need some help with my shopping cart, i am adding item on the basket, but when i click on the basket for those item added its empty.
// basket.php
$quantity) {
$sql = "SELECT * FROM products WHERE productID = '$product_id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
die('Error getting product details: ' . mysqli_error($conn));
}
$product = mysqli_fetch_assoc($result);
$product['quantity'] = $quantity;
$cart_items[] = $product;
}
// Close the database connection
mysqli_close($conn);
}
?>
Shopping Cart
<
Shopping Cart
Image
Product Name
Unit Price
Quantity
Discount
Amount
![<?php echo $cart_item['product_name']; ?>](<?php echo $cart_item['image']; ?>)
$
$
Your cart is empty.
// get_product_details.php
$quantity) {
$sql = "SELECT * FROM products WHERE productID = '$product_id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
die('Error getting product details: ' . mysqli_error($conn));
}
$product = mysqli_fetch_assoc($result);
$product['quantity'] = $quantity;
$cart_items[] = $product;
}
// Close the database connection
mysqli_close($conn);
?>
Shopping Cart
rel="stylesheet">
Product Name
Product ID
Unit Price
Quantity
Discount
Amount
$cart_item): ?>
$
$
// jquery code
function addToCart(productId, quantity) {
var cart = getCart();
if (cart.hasOwnProperty(productId)) {
var newQuantity = parseInt(quantity);
if (newQuantity <= 0) {
delete cart[productId];
} else {
cart[productId] = newQuantity;
}
} else {
cart[productId] = parseInt(quantity);
}
saveCart(cart);
updateCartCount();
calculate_total();
// Display message
var productName = $('#product-name-' + productId).text();
$('#cart-message').text(quantity + 'x ' + productName + ' added to cart').show();
setTimeout(function() {
$('#cart-message').fadeOut('slow');
}, 3000);
}
function calculate_total() {
var cart = getCart();
var total = 0;
for (var productId in cart) {
var product = getProductId(productId);
var price = parseFloat(product.price);
var quantity = parseInt(cart[productId]);
// Send request to server to get discount for the product
$.ajax({
url: 'get_product_details.php',
data: {productId: productId},
dataType: 'json',
success: function(data) {
// Update HTML elements with discount and amount
var discount = parseFloat(data.discount);
var amount = price * quantity * (1 - discount);
$('#discount-' + productId).text((discount * 100).toFixed(0) + '%');
$('#amount-' + productId).text('$' + amount.toFixed(0));
},
error: function(xhr, textStatus, errorThrown) {
console.log('Error getting product details: ' + errorThrown);
}
});
Jay Krishna ReddyPosted Apr 25, 2023, 12:04 PM
Based on the code you provided, it seems that you are trying to display the items in the shopping cart on the
basket.phppage. However, you mentioned that when you click on the basket, it shows as empty even though you have added items to it. Here are some possible issues and solutions:session_start()at the beginning of bothbasket.phpandget_product_details.phpfiles, before any output (e.g., HTML tags or echo statements). Also, ensure that$_SESSION['cart']is properly initialized as an array before adding any items to it. For example, you can add the following code at the beginning ofget_product_details.phpfile:Incorrect usage of product IDs in the session: Make sure that the product IDs used to add items to the cart and retrieve items from the cart are the same. In your code, you are using
$product_idas the key in the$_SESSION['cart']array when adding items to the cart, and as the key to retrieve items from the$_SESSION['cart']array inget_product_details.php. If the product IDs used are not the same, it will result in an empty cart. Make sure that you are using the same product IDs consistently throughout your code.Issue with SQL queries: Check if the SQL queries used to retrieve product details from the database are correct and returning the expected results. If there is an issue with the SQL queries, it could result in an empty cart. Make sure that the database connection is established correctly and that the product details are being retrieved accurately from the database.
Incorrect file paths or URLs: Double-check that the file paths or URLs used to include the
basket.phpandget_product_details.phpfiles in your code are correct. If the file paths or URLs are incorrect, it could result in an empty cart.Browser caching issue: Sometimes, browsers may cache the pages and display a cached version instead of the updated version. Try clearing your browser cache and refreshing the page to see if the cart items are displayed correctly.
Debugging: You can add some
echoorvar_dumpstatements in your code to debug and check the values of variables, arrays, and database queries at different stages of execution. This can help you identify any issues or inconsistencies in your code.By checking these possible issues and applying the appropriate solutions, you should be able to resolve the issue of items not appearing in your shopping cart.
Rajkiran SwainPosted Apr 25, 2023, 12:03 PM
Dear Gcobani Mkontwana,
Don't mind, but it seems like you are completing your office work with us. I have been following your forum posts for the last 15 days, and you have been posting your entire project here, which is creating a mess and causing a decline in application performance. Please try to post only the exact issue and avoid posting lengthy code that is difficult to understand and resolve. As we respect everyone's time, I kindly request that you consider this matter.
I also request the admin to look into this matter.
Thanks,
Rajkiran