我需要为购物车创建动态变量,以便发送到支付网关。
每个变量的形式为:
$parameters['Product Name: Qty = '. $cart_qty] = $subtotal;
因此,如果我订购两种产品,参数列表将是:
$parameters['Product1: Qty = 2'] = 24.00;
$parameters['Product2: Qty = 3'] = 60.00;
等等
如何通过循环遍历每个产品将这些参数生成为字符串?
例如:
while ($data_cart = mysql_fetch_array($result_cart)) {
$prodName = $data_product['prodName'];
$unitPrice = number_format((float)$data_product['priceRetail'], 2, '.', '');
$cart_qty = $data_cart['qty'];
$subtotal = $cart_qty * $unitPrice;
$parameter .= ??? // <-- How to create a dynamic string here to output later
}
然后我以后该如何将其写入页面,是回显还是打印?
在构建完整数组后,尝试使用类似http_build_query()
的查询构建函数(不过,根据您的示例,您的键有报价问题):
<?php
$parameters['Product1: Qty = 1'] = '24.00';
$parameters['Product3: Qty = 3'] = '24.00';
$parameters['Product2: Qty = 5'] = '24.00';
$parameters['Product5: Qty = 1'] = '24.00';
echo http_build_query($parameters);
?>
给你:
Product1%3A+Qty+%3D+1=24.00&Product3%3A+Qty+%3D+3=24.00&Product2%3A+Qty+%3D+5=24.00&Product5%3A+Qty+%3D+1=24.00