我在我的网站上使用 Paypal 处理付款。 Paypal 返回 post 数组,如
[item_number1] =12
[item_name1] = My product name
[quantity1] =3
[item_number2] =14
[item_name2] = My product name2
[quantity2] =5
[num_cart_items] 2
现在在这里插入步骤,我只是在兜圈子,一事无成
然后运行for循环
for($i = 1;$i <= $num_cart_items ;$i++){
$x = 'item_number' . $i;
$y = 'item_name' . $i;
$z = 'quantity' . $i;
$new_amount = $row['stock_quantity'] - $$z;
$db->update1_by_match('cart_products','stock_quantity',$new_amount,'id',$$x);
}
我在处理这些变量时遇到了麻烦。有更好的方法吗?
谢谢 安德鲁
最佳答案
我会更改您修改后的代码以直接使用 $_POST,例如
for($i = 1;$i <= $_POST['num_cart_items'] ;$i++){
$item_number= intval($_POST['item_number' . $i]);
$item_quantity= intval($_POST['quantity' . $i]);
printf("DEBUG: item %d item:%d quantity:%d<br>", $i, $item_number, $item_quantity);
$new_amount = $row['stock_quantity'] - $item_quantity;
$db->update1_by_match('cart_products','stock_quantity',$new_amount,'id', $item_number);
}
诊断输出应该可以帮助您改进事情的发展方向。
早期问题 - 下面的注释指的是在对问题进行完整修订之前的问题。
你真正需要的是一个数组,而不是试图使用变量变量
$item_numbers=array(24, 16);
foreach ($item_numbers as $item_number) {
$result = $db->get_cols_by_match('cart_products','stock_quantity','id', $item_number);
}
按照你原来的方式去做,这样的事情可能会澄清
$item_number1='24';
$item_number2='16';
$num_cart_items = 2
for($i = 1;$i <= $num_cart_items ;$i++){
$varname='item_number' . $i;
printf("DEBUG: %s = %s<br>", $varname, $$varname);
$result = $db->get_cols_by_match('cart_products','stock_quantity','id', $$varname);
}
$$varname 是一个 variable variable 的例子,但在您的情况下,数组以更清晰的方式声明您的意图。
关于php - 使用来自 paypal 的 post 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1807827/