2

I currently have a table populated from an array. The issue i'm having is storing the products on "Add to Cart"

    <?php
session_start();
$products = array(
array('SKU'=>'test1', 'name'=>'ProductTest1', 'Price'=>10.00),
array('SKU'=>'test2', 'name'=>'ProductTest2', 'Price'=>11.00),
array('SKU'=>'test3', 'name'=>'ProductTest3', 'Price'=>12.00)
);

if(isset($_GET['action' && $_get['action'] == "addToCart"))
{
$id = $_GET['product']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}
else
{


?>

Below is also the table php which populates correctly.

<?php foreach ($products as $key => $product) {
echo '<tr>';
echo '<td>' . $product['SKU'] . '</td>';
echo '<td>' . $product['name'] . '</td>';
echo '<td>' . '&pound;'. number_format($product['Price'],2) . '</td>';
echo '<td><a href="?action=addToCart&product='. $key.'">Add To Cart </a></td>';
echo '</tr>';
}
?>

I can't figure out how to store the name and price of the items using the GET function, which should take the value from the URL.

Any help would be great!

4

1 回答 1

0

将元素从复制$products到会话变量中以获取名称、SKU 和价格。

if(isset($_SESSION['cart'][$id])){
    $_SESSION['cart'][$id]['quantity']++;
} else {
    $_SESSION['cart'][$id] = $products[$id];
    $_SESSION['cart'][$id]['quantity'] = 1;
}

现在其他脚本可以访问$_SESSION['cart'][$id]['name']$_SESSION['cart'][$id]['Price'].

另一种选择是将$products定义放在包含文件中,该文件由其他页面加载以获取此信息。

最终,您可能希望将您的产品信息放入数据库中,这两个页面都可以访问该数据库。

于 2013-06-12T19:01:50.553 回答