php贝宝沙箱动态列表


php paypal sandbox dynamic list

我正在尝试通过将沙盒与多个产品一起使用来制作贝宝按钮。当我在购物车上添加产品并按下贝宝按钮时,它运行良好。然而,如果我在购物车上制作两到三种产品并按下按钮,它会显示信息,"您的购物车是空的"。我认为这个问题出现在动态列表中。你能帮我一下吗?动态列表使用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;代替$x = $i + 1;。或者只做$i++,在输入的形成代码中使用$i而不是$x