我正在尝试通过对多种产品使用沙箱来制作 Paypal 按钮。当我在购物车中添加产品并按下 paypal 按钮时,它工作正常。但是,如果我在购物车上制作两个或三个产品并按下按钮,它出现消息,“您的购物车是空的”。我认为问题出在动态列表中。你能帮我点忙吗?动态列表使用 foreach 语句。老实说,在这个 foreach 语句中,其他代码工作正常。请帮我。谢谢。
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart"/>
<input type="hidden" name="upload" value="1"/>
<input type="hidden" name="business" value="wholee1@googlemail.com"/>
<input type="hidden" name="notify_url" value="XXX">
<input type="hidden" name="return" value="http://localhost/eshopProject/home.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="http://localhost/eshopProject/android.php">
<input type="hidden" name="lc" value="UK">
<input type="hidden" name="currency_code" value="GBP">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
<?php echo $pp_checkout_btn; ?>
</form>
//added more php sources here
<?php
//if user attempts to add something to the cart from the product page
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
//if cart is empty or not set, run it
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
}
else{
//if cart has at least one item in it
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key, $value) = each($each_item)){
if($key =="item_id" && $value == $pid){
//this item is in cart already, then it can be adjusted the quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1,
array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
}
}
}
if($wasFound == false){
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>
//php code
$pp_checkout_btn = '';
$i = 0;
foreach($_SESSION["cart_array"] as $each_item){
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM product WHERE productId='$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$category = $row["product_category"];
$product_name = $row["product_name"];
$brand = $row["product_brand"];
$price = $row["product_price"];
}
$priceTotal = $price * $each_item['quantity'];
$cartTotal = $priceTotal + $cartTotal;
$x = $i + 1;
//dynamic Checkout Button
$pp_checkout_btn = '<input type="hidden" name="item_name_'.$x.'" value="'.$product_name.'">
<input type="hidden" name="amount_'.$x.'" value="'.$price.'">
<input type="hidden" name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
}
您似乎忘记了在 foreach 中递增 $i
。这就是为什么您一直只生成一个项目($x
始终等于 1,除第一个元素外的所有元素都将被忽略)。
我建议将 $x = $i + 1;
替换为 $x =++$i;
。或者只是执行 $i++
并在输入的形成代码中使用 $i
而不是 $x
。
我是一名优秀的程序员,十分优秀!