-3

朋友写了一个小脚本来获取所选商品的商品id和商品数量。这里是:

$product_id_string = $_POST['custom'];
$product_id_string = rtrim($product_id_string, ","); 
$id_values = array();
$id_str_array = explode(",", $product_id_string);
$fullAmount = 0;
foreach ($id_str_array as $key => $value) {

$id_quantity_pair = explode("-", $value);
$product_id = $id_quantity_pair[0]; // product ID
$product_quantity = $id_quantity_pair[1]; // product quantity 
}

我是怎么理解的...产品ID及其数量是从自定义变量中读取的?问候!

4

1 回答 1

0

Exactly. The custom variable is a string "aaa,bbb,ccc,ddd". Explode converts this into an array (in my example 4 elements.) foreach() iterates them, so that everything inside it will be executed 4 times, where $value takes the values aaa, bbb and so on.

$_POST['custom'] needs to be id-quantity,id-quantity,id-quantity, e.g.

<input type="hidden" name="custom" value="A123-4,B456-1">

That would mean you bought 4 times the item A123 and once the item B456.

于 2013-05-31T12:28:47.097 回答