我有以下函数,它们基于 shoppingCart 类生成购物车。
function render_shopping_cart()
{
$shopping_cart = get_shopping_cart();
$output = "<table class='shoppingCart'>
<tr>
<th>
Course
</th>
<th>
Valid Until
</th>
<th>
Quantity
</th>
<th>
Price
</th>
</tr>
";
$line_item_counter = 1;
foreach ($shopping_cart->GetItems() as $product_id)
{
$output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);
$line_item_counter++;
}
//$output .= render_shopping_cart_shipping_row($shopping_cart);
$output .= render_shopping_cart_total_row($shopping_cart);
$output .="</table>";
return $output;
}
function render_shopping_cart_total_row(ShoppingCart $shopping_cart)
{
return "<tr>
<td>
</td>
<td>
</td>
<td>
Total:
</td>
<td>
<input type='hidden' name='no_shipping' value='0'>
$".$shopping_cart->GetTotal()."
</td>
</tr>";
}
function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id, $line_item_counter)
{
$quantity = $shopping_cart->GetItemQuantity($product_id);
$amount = $shopping_cart->GetItemCost($product_id);
$unit_cost = get_item_cost($product_id);
$shipping_amount = $shopping_cart->GetItemShippingCost($product_id);
$title = get_item_title($product_id);
$validUntil = expiration();
return "
<tr>
<td>
$title
<input type='hidden' name='item_name_$line_item_counter' value='$product_id' />
</td>
<td>
$validUntil
<input type='hidden' name='quantity_$line_item_counter' value='$validUntil' />
</td>
<td>
$quantity
<input type='hidden' name='quantity_$line_item_counter' value='$quantity' />
</td>
<td>
$$amount
<input type='hidden' name='amount_$line_item_counter' value='$unit_cost' />
</td>
</tr>
";
}
我想弄清楚的是如何获取$product_id
为每个项目生成的;特别是这部分$title
<input type='hidden' name='item_name_$line_item_counter' value='$product_id' />
并将其放入 $sqlProducts 数组或将它们分别插入到单个 mysql 字段中product1, product2, product3, etc
我已经添加
$sqlProducts = serialize($product_id);
echo unserialize($sqlProducts);
到 line_item 柜台看起来像
$line_item_counter = 1;
foreach ($shopping_cart->GetItems() as $product_id)
{
$output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);
$line_item_counter++;
$sqlProducts = serialize($product_id);
echo unserialize($sqlProducts); // I've made $abc = unserialize($sqlProducts) as well and it hasn't worked.
}
它们在函数内回显得很好,但我无法访问函数外的变量。提交表单后,我需要了解如何传递$sqlProducts
给 process.php。或者,如果有另一种方法可以$product_id
在表单发布后获取生成的 s 数组并将它们插入到 mysql 表中。
更新
我努力了
$sqlProducts = serialize($product_id);
$_SESSION['sqlProducts'] = unserialize($sqlProducts);
这有效,但只回显数组中的最后一项而不是整个内容。
虽然在页面内,
$sqlProducts = serialize($product_id);
$_SESSION['sqlProducts'] = unserialize($sqlProducts);
echo $_SESSION['sqlProducts'];
工作正常
目前,表单发布到 process.php 有一个简单的行,echo $_SESSION
或者echo $sqlProducts
不幸的是,当我 echo 时$sqlProducts
,该值是未定义的,如果我 echo$_SESSION['sqlProducts']
我只得到数组中的最后一项
以下为其他有此问题的人推荐的解决方案
我重写了函数并创建了数组:
function createCart()
{
//Create a new cart as a session variable with the value being an array
$_SESSION['paypalCart'] = array();
}
function insertToCart($productID, $productName, $price, $qty = 1)
{
//Function is run when a user presses an add to cart button
//Check if the product ID exists in the paypal cart array
if(array_key_exists($productID, $_SESSION['paypalCart']))
{
//Calculate new total based on current quantity
$newTotal = $_SESSION['paypalCart'][$productID]['qty'] + $qty;
//Update the product quantity with the new total of products
$_SESSION['paypalCart'][$productID]['qty'] = $newTotal;
}
else
{
//If the product doesn't exist in the cart array then add the product
$_SESSION['paypalCart'][$productID]['ID'] = $productID;
$_SESSION['paypalCart'][$productID]['name'] = $productName;
$_SESSION['paypalCart'][$productID]['price'] = $price;
$_SESSION['paypalCart'][$productID]['qty'] = $qty;
}
}
现在我可以使用
<?php
if (isset ($_SESSION['paypalCart']))
{
foreach($_SESSION['paypalCart'] as $product)
{
$custom .= $product['name'].", ";
}
}
?>
<input type="hidden" name="custom" value="<?php echo $custom?>">
请记住$custom
在调用变量之前在页面中的某处进行初始化(或您正在使用的任何内容),例如:$custom:"";