PHP 添加到购物车并且模态显示为空
PHP adding to cart and modal display empty
我是新来的PHP。我正在建立电子商务网站。在购物车中。如果我尝试将其添加的产品添加到购物车。模态没有显示。其次是它的补充。模态显示成功。现在我想在单击产品中的“添加到购物车”时显示模态。
总库存:15
URL: https://www.bellastudio.pk/new/products.php?p_slug=embroidery-shalwar-dupatta-2
这是插入cart.php提前谢谢你
PHP
$stmt = $host->prepare("SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size");
$stmt->bindValue(':itemno', $itemno);
$stmt->bindValue(':size', $size);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if($qty <= $row['stockqty']){
if($qty == 0)
{
echo "<p>Please enter a quantity</p>";
}else
{
$product = array("name" => $name,
"price" => $price,
"item_no" => $itemno,
"qty" => $qty,
"color" => $color,
"size" => $size,
"stockitemno" => $stockitemno,
"sleeves" => $sleeves,
"neck" => $neck,
"stockqty" => $row['stockqty']);
// check if product is already in array - if it is just update quantity
if(count($_SESSION['cart']) > 0)
{
foreach($_SESSION['cart'] as $key => $cart)
{
if(in_array($stockitemno, $cart))
{
$exists = 1;
$check = $_SESSION['cart'][$key]['qty'] + $qty;
// add qty in cart with qty from post together
// then check if the total is equal to or less
// than your stockqty - if yes add qty together‚
if($check <= $_SESSION['cart'][$key]['stockqty']){
$_SESSION['cart'][$key]['qty'] += $qty;
}
break;
}
}
}
foreach($_SESSION['cart'] as $key => $cart)
{
if($_SESSION['cart'][$key]['qty'] >= $_SESSION['cart'][$key]['stockqty'])
{
echo "No More Stock";
}else
{
echo $name . ' (' . $size . ')';
echo "<br />";
echo "<p>was successfully added to your shopping cart</p>";
}
}
// if exists = 1
if($exists !== 1)
{
$_SESSION['cart'][] = $product;
}
}
}
else
{
echo "<p>The Requested quantity for this product is not available.</p>";
}
}
} catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$host = null;
$cart_count = count($_SESSION['cart']);
?>
Ajax
<script type="text/javascript">
$(document).ready(function()
{
function load_cart_data()
{
$.ajax({
url:"asset/includes/fetch_cart1.php",
method:"POST",
success:function(data)
{
$('.cart_details').html(data);
$('.badge1').text(data.total_item);
}
});
}
$(document).on('click','#add_to_cart',function (e) {
var cart_count = $('.navbar-tool-badge').text();
cart_count = parseInt(cart_count);
if($('input[type=radio][name=size]:checked').length == 0)
{
$('.msg').html('Please choose size.');
return false;
} else {
var product_name = $('#hidden-name').val();
var product_price = $('#hidden-price').val();
var product_itemno = $('#itemno').val();
var product_quantity = $('.quantity').val();
var product_color = $('#color').val();
var product_size = $("input[name='size']:checked").val();
var sleeves = $('#sleeves').val();
var neck = $('#neck').val();
e.preventDefault();
$.ajax
({
type: "POST",
url: "asset/includes/insertcart.php",
data:{product_name:product_name, product_price:product_price, product_itemno:product_itemno , product_quantity:product_quantity, product_color:product_color, product_size:product_size, sleeves:sleeves, neck:neck},
cache: false,
success: function(response)
{
load_cart_data();
$("#getCode").html(response);
$("#myModal").modal('show');
// remove cart count
$('.navbar-tool-badge').html(cart_count + 1);
}
});
}
});
$(document).on('click', '.delete', function(){
var item_no = $(this).attr("id");
var cart_count = $('.navbar-tool-badge').text();
cart_count = parseInt(cart_count);
$.ajax({
url:"asset/includes/delete_item.php",
method:"POST",
data:{item_no:item_no},
success:function(data)
{
if(data){
load_cart_data();
$('#cart-popover').popover('hide');
}
// remove cart count
$('.navbar-tool-badge').html(cart_count - 1);
}
})
});
});
</script>
尝试以下逻辑,希望对您有所帮助:
if (count($_SESSION['cart']) > 0) {
foreach ($_SESSION['cart'] as $key => $cart)
{
if (in_array($stockitemno, $cart)) {
$exists = 1;
$check = $_SESSION['cart'][$key]['qty'] + $qty;
// add qty in cart with qty from post together
// then check if the total is equal to or less
// than your stockqty - if yes add qty together‚
if($check <= $_SESSION['cart'][$key]['stockqty']){
$_SESSION['cart'][$key]['qty'] += $qty;
}
break;
}
foreach($_SESSION['cart'] as $key => $cart)
{
if ($_SESSION['cart'][$key]['qty'] >= $_SESSION['cart'][$key]['stockqty']) {
echo "No More Stock";
} else {
echo $name . ' (' . $size . ')';
echo "<br />";
echo "<p>was successfully added to your shopping cart</p>";
}
}
// if exists = 1
if($exists !== 1)
{
$_SESSION['cart'][] = $product;
}
}
} else {
echo "<p>Total Stock: " . $qty . "</p>";
}
查看提供的代码后,我发现问题出在 PHP。请看下面我的逻辑。
<?php
try {
$stmt = $host->prepare( "SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size" );
$stmt->bindValue( ':itemno', $itemno );
$stmt->bindValue( ':size', $size );
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
// Current stock quantity
$stockqty = $row[ 'stockqty' ];
// No stock, bail early
if ( !$stockqty ) {
echo '<p>No available stock for that item.</p>';
}
// User didn't provide a quantity value
else if ( !$qty ) {
echo '<p>Please enter a quantity</p>';
}
// Lets add to the cart
else {
// Find product in cart
$cartindex = array_search( $itemno, array_column( $_SESSION[ 'cart' ], 'item_no' ) );
// Product info
$product = null;
// If already exists in cart
if ( $cartindex !== false ) {
$product = $_SESSION[ 'cart' ][ $cartindex ];
}
// Does not exit in cart, create new product info
else {
$product = [
'name' => $name,
'price' => $price,
'item_no' => $itemno,
'qty' => 0, // Default to none added
'color' => $color,
'size' => $size,
'stockitemno' => $stockitemno,
'sleeves' => $sleeves,
'neck' => $neck,
'stockqty' => $stockqty
];
}
// Track how many items were able to be added to the cart
$totaladded = 0;
// If users full amount is available
if ( ( $product[ 'qty' ] + $qty ) <= $stockqty ) {
$totaladded = $qty;
}
// Else add all available stock
else if ( $stockqty - $product[ 'qty' ] ) {
$totaladded = ( $stockqty - $product[ 'qty' ] );
}
// If we were able to add new items to cart
if ( $totaladded ) {
// Update product new qty
$product[ 'qty' ] += $totaladded;
// Update cart item
if ( $cartindex !== false ) {
$_SESSION[ 'cart' ][ $cartindex ] = $product;
}
// Add new item to cart
else {
$_SESSION[ 'cart' ][] = $product;
}
// Example: 5 item (size) were successfully added to your shopping cart.
printf( '<p><strong>%d</strong> %s (%s)<br>was successfully added to your shopping cart.</p>', $totaladded, $name, $size );
} else {
echo '<p>Not enough stock</p>';
}
}
}
} catch ( PDOException $e ) {
echo 'Error: ' . $e->getMessage();
}
$host = null;
$cart_count = count( $_SESSION[ 'cart' ] );
我是新来的PHP。我正在建立电子商务网站。在购物车中。如果我尝试将其添加的产品添加到购物车。模态没有显示。其次是它的补充。模态显示成功。现在我想在单击产品中的“添加到购物车”时显示模态。 总库存:15
URL: https://www.bellastudio.pk/new/products.php?p_slug=embroidery-shalwar-dupatta-2
这是插入cart.php提前谢谢你
PHP
$stmt = $host->prepare("SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size");
$stmt->bindValue(':itemno', $itemno);
$stmt->bindValue(':size', $size);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if($qty <= $row['stockqty']){
if($qty == 0)
{
echo "<p>Please enter a quantity</p>";
}else
{
$product = array("name" => $name,
"price" => $price,
"item_no" => $itemno,
"qty" => $qty,
"color" => $color,
"size" => $size,
"stockitemno" => $stockitemno,
"sleeves" => $sleeves,
"neck" => $neck,
"stockqty" => $row['stockqty']);
// check if product is already in array - if it is just update quantity
if(count($_SESSION['cart']) > 0)
{
foreach($_SESSION['cart'] as $key => $cart)
{
if(in_array($stockitemno, $cart))
{
$exists = 1;
$check = $_SESSION['cart'][$key]['qty'] + $qty;
// add qty in cart with qty from post together
// then check if the total is equal to or less
// than your stockqty - if yes add qty together‚
if($check <= $_SESSION['cart'][$key]['stockqty']){
$_SESSION['cart'][$key]['qty'] += $qty;
}
break;
}
}
}
foreach($_SESSION['cart'] as $key => $cart)
{
if($_SESSION['cart'][$key]['qty'] >= $_SESSION['cart'][$key]['stockqty'])
{
echo "No More Stock";
}else
{
echo $name . ' (' . $size . ')';
echo "<br />";
echo "<p>was successfully added to your shopping cart</p>";
}
}
// if exists = 1
if($exists !== 1)
{
$_SESSION['cart'][] = $product;
}
}
}
else
{
echo "<p>The Requested quantity for this product is not available.</p>";
}
}
} catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$host = null;
$cart_count = count($_SESSION['cart']);
?>
Ajax
<script type="text/javascript">
$(document).ready(function()
{
function load_cart_data()
{
$.ajax({
url:"asset/includes/fetch_cart1.php",
method:"POST",
success:function(data)
{
$('.cart_details').html(data);
$('.badge1').text(data.total_item);
}
});
}
$(document).on('click','#add_to_cart',function (e) {
var cart_count = $('.navbar-tool-badge').text();
cart_count = parseInt(cart_count);
if($('input[type=radio][name=size]:checked').length == 0)
{
$('.msg').html('Please choose size.');
return false;
} else {
var product_name = $('#hidden-name').val();
var product_price = $('#hidden-price').val();
var product_itemno = $('#itemno').val();
var product_quantity = $('.quantity').val();
var product_color = $('#color').val();
var product_size = $("input[name='size']:checked").val();
var sleeves = $('#sleeves').val();
var neck = $('#neck').val();
e.preventDefault();
$.ajax
({
type: "POST",
url: "asset/includes/insertcart.php",
data:{product_name:product_name, product_price:product_price, product_itemno:product_itemno , product_quantity:product_quantity, product_color:product_color, product_size:product_size, sleeves:sleeves, neck:neck},
cache: false,
success: function(response)
{
load_cart_data();
$("#getCode").html(response);
$("#myModal").modal('show');
// remove cart count
$('.navbar-tool-badge').html(cart_count + 1);
}
});
}
});
$(document).on('click', '.delete', function(){
var item_no = $(this).attr("id");
var cart_count = $('.navbar-tool-badge').text();
cart_count = parseInt(cart_count);
$.ajax({
url:"asset/includes/delete_item.php",
method:"POST",
data:{item_no:item_no},
success:function(data)
{
if(data){
load_cart_data();
$('#cart-popover').popover('hide');
}
// remove cart count
$('.navbar-tool-badge').html(cart_count - 1);
}
})
});
});
</script>
尝试以下逻辑,希望对您有所帮助:
if (count($_SESSION['cart']) > 0) {
foreach ($_SESSION['cart'] as $key => $cart)
{
if (in_array($stockitemno, $cart)) {
$exists = 1;
$check = $_SESSION['cart'][$key]['qty'] + $qty;
// add qty in cart with qty from post together
// then check if the total is equal to or less
// than your stockqty - if yes add qty together‚
if($check <= $_SESSION['cart'][$key]['stockqty']){
$_SESSION['cart'][$key]['qty'] += $qty;
}
break;
}
foreach($_SESSION['cart'] as $key => $cart)
{
if ($_SESSION['cart'][$key]['qty'] >= $_SESSION['cart'][$key]['stockqty']) {
echo "No More Stock";
} else {
echo $name . ' (' . $size . ')';
echo "<br />";
echo "<p>was successfully added to your shopping cart</p>";
}
}
// if exists = 1
if($exists !== 1)
{
$_SESSION['cart'][] = $product;
}
}
} else {
echo "<p>Total Stock: " . $qty . "</p>";
}
查看提供的代码后,我发现问题出在 PHP。请看下面我的逻辑。
<?php
try {
$stmt = $host->prepare( "SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size" );
$stmt->bindValue( ':itemno', $itemno );
$stmt->bindValue( ':size', $size );
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
// Current stock quantity
$stockqty = $row[ 'stockqty' ];
// No stock, bail early
if ( !$stockqty ) {
echo '<p>No available stock for that item.</p>';
}
// User didn't provide a quantity value
else if ( !$qty ) {
echo '<p>Please enter a quantity</p>';
}
// Lets add to the cart
else {
// Find product in cart
$cartindex = array_search( $itemno, array_column( $_SESSION[ 'cart' ], 'item_no' ) );
// Product info
$product = null;
// If already exists in cart
if ( $cartindex !== false ) {
$product = $_SESSION[ 'cart' ][ $cartindex ];
}
// Does not exit in cart, create new product info
else {
$product = [
'name' => $name,
'price' => $price,
'item_no' => $itemno,
'qty' => 0, // Default to none added
'color' => $color,
'size' => $size,
'stockitemno' => $stockitemno,
'sleeves' => $sleeves,
'neck' => $neck,
'stockqty' => $stockqty
];
}
// Track how many items were able to be added to the cart
$totaladded = 0;
// If users full amount is available
if ( ( $product[ 'qty' ] + $qty ) <= $stockqty ) {
$totaladded = $qty;
}
// Else add all available stock
else if ( $stockqty - $product[ 'qty' ] ) {
$totaladded = ( $stockqty - $product[ 'qty' ] );
}
// If we were able to add new items to cart
if ( $totaladded ) {
// Update product new qty
$product[ 'qty' ] += $totaladded;
// Update cart item
if ( $cartindex !== false ) {
$_SESSION[ 'cart' ][ $cartindex ] = $product;
}
// Add new item to cart
else {
$_SESSION[ 'cart' ][] = $product;
}
// Example: 5 item (size) were successfully added to your shopping cart.
printf( '<p><strong>%d</strong> %s (%s)<br>was successfully added to your shopping cart.</p>', $totaladded, $name, $size );
} else {
echo '<p>Not enough stock</p>';
}
}
}
} catch ( PDOException $e ) {
echo 'Error: ' . $e->getMessage();
}
$host = null;
$cart_count = count( $_SESSION[ 'cart' ] );