基于会话的购物车
session based shopping cart
最初我让它工作正常,通过 id
传递产品 array_push()
函数,从 addtocart.php
到 Shoppingcart.php
显示项目。但是当我添加更多 variables/into array_push()
函数时,除了单个产品 id
$_GET['id']
,...在接收此数组的下一页上,它给了我一个错误。
问题是:
在最初的 $sql
查询中,它正在获取 id
它需要显示来自 array_push()
的产品信息并将其显示到 Shoppingcart.php
,但是当我添加更多 variables/info 到 push_array()
我得到一个错误。因为它混淆了 $sql
查询,因为 WHERE id IN
子句...... ID
仍然在那里,现在连同其他信息($_GET['size']
& $_GET['qty']
), 我只是不知道如何访问它...
如何将更多信息添加到推送数组中,但如何定义它以便我可以为我的 $sql
查询获取 id
,以获取产品信息,但也可以访问到我的 while()
循环的 size
& Qty
。
addtocart.php
array_push($_SESSION['cart'], $_GET['id']); //working MAIN
header('Location:shoppingCart.php');
How 2: array_push($_SESSION['cart'], $_GET['id'], $_GET['size'], $_GET['qty']);
//Not Working
shoppingcart.php
<?php
$whereIn = implode(',', $_SESSION['cart']); //working MAIN
$sql = " SELECT * FROM inventory WHERE id IN ($whereIn) "; ?>
<?php while($row = mysql_fetch_array($result)) { ?>
<td valign="top">
<div id="sc_itemBox">
<p class="sc_itemBox_iTEXT"><strong>SIZE:</strong> “”XL?? <em>(Extra Large??)</em></p>
<div id="sc_itemBox_img"><img src="<?php echo $row['imgThumb'];?>" /></div>
<p class=<p class="sc_itemBox_iTEXT"><strong>STYLE#</strong><?php echo $row['styleNUM']; ?> </p>
</div>
</td>
<?php } ?>
我认为让您的 $_SESSION['cart']
稍微复杂一点可能会有所帮助。尝试将您的 id
等分离到他们自己的数组中。也许是这样的。
addtocart.php
if(isset($_SESSION['cart'])) {
// Store just ids
// Use array_unique to filter this array
$_SESSION['cart']['id'] = array_unique($_SESSION['cart']['id']);
// Save new cart array with that items basics
$_SESSION['cart'][$_GET['id']]['size'][] = $_GET['size'];
$_SESSION['cart'][$_GET['id']]['qty'][] = $_GET['qty'];
header('Location:shoppingCart.php');
exit;
}
shoppingcart.php
// Implode just the id array, no other array in the cart session
// (make sure your addtocart checks for is_numeric so you don't get someone
// injecting sql junk into your db
$whereIn = implode(',', $_SESSION['cart']['id']);
$sql = "SELECT * FROM inventory WHERE id IN ($whereIn)";
编辑: 这里有一种方法可以让您将商品分成不同的尺寸(以防您添加多个具有相同 ID 但尺寸和数量不同的商品) :
function AddToCart()
{
// Confirm that an id is being added
// I am assuming there is an "add" trigger
if(isset($_GET['add']) && is_numeric($_GET['id'])) {
// Create the item in the cart
// Record size
if(isset($_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty']))
// Notice here that if there is already this item in the cart
// with the exact same size, it will sum
$_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty'] += $_GET['qty'];
else
// If not in cart at this size, it will add qty
$_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty'] = $_GET['qty'];
}
}
// Fetch ids for your query
function FetchItems()
{
if(isset($_SESSION['cart'])) {
foreach($_SESSION['cart'] as $itemcode => $array) {
$items[] = $itemcode;
}
return (isset($items))? $items:false;
}
}
// Start the session
session_start();
// Add to cart
AddToCart();
// This will fetch your ids for your query
$mysqlIds = implode(",",FetchItems());
echo '<pre>';
print_r($mysqlIds);
echo '</pre>'; ?>
<!-- These are just for testing. Will generate different sizes and qty-->
<a href="?add=true&id=1&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 1</a>
<a href="?add=true&id=2&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 2</a>
<a href="?add=true&id=3&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 3</a>
会给你:
// Session array after adding items to it.
Array
(
[cart] => Array
(
[2] => Array
(
[size] => Array
(
[21] => Array
(
[qty] => 1
)
[9] => Array
(
[qty] => 2
)
[8] => Array
(
[qty] => 0
)
[7] => Array
(
[qty] => 20
)
[2] => Array
(
[qty] => 5
)
)
)
[3] => Array
(
[size] => Array
(
[9] => Array
(
[qty] => 3
)
[1] => Array
(
[qty] => 0
)
[7] => Array
(
[qty] => 4
)
[10] => Array
(
[qty] => 6
)
[3] => Array
(
[qty] => 20
)
[2] => Array
(
[qty] => 10
)
[12] => Array
(
[qty] => 2
)
[6] => Array
(
[qty] => 10
)
)
)
[1] => Array
(
[size] => Array
(
[11] => Array
(
[qty] => 1
)
[3] => Array
(
[qty] => 3
)
[2] => Array
(
[qty] => 2
)
)
)
)
)
// This is for the ids for your mysql query
2,3,1
最初我让它工作正常,通过 id
传递产品 array_push()
函数,从 addtocart.php
到 Shoppingcart.php
显示项目。但是当我添加更多 variables/into array_push()
函数时,除了单个产品 id
$_GET['id']
,...在接收此数组的下一页上,它给了我一个错误。
问题是:
在最初的 $sql
查询中,它正在获取 id
它需要显示来自 array_push()
的产品信息并将其显示到 Shoppingcart.php
,但是当我添加更多 variables/info 到 push_array()
我得到一个错误。因为它混淆了 $sql
查询,因为 WHERE id IN
子句...... ID
仍然在那里,现在连同其他信息($_GET['size']
& $_GET['qty']
), 我只是不知道如何访问它...
如何将更多信息添加到推送数组中,但如何定义它以便我可以为我的 $sql
查询获取 id
,以获取产品信息,但也可以访问到我的 while()
循环的 size
& Qty
。
addtocart.php
array_push($_SESSION['cart'], $_GET['id']); //working MAIN
header('Location:shoppingCart.php');
How 2: array_push($_SESSION['cart'], $_GET['id'], $_GET['size'], $_GET['qty']);
//Not Working
shoppingcart.php
<?php
$whereIn = implode(',', $_SESSION['cart']); //working MAIN
$sql = " SELECT * FROM inventory WHERE id IN ($whereIn) "; ?>
<?php while($row = mysql_fetch_array($result)) { ?>
<td valign="top">
<div id="sc_itemBox">
<p class="sc_itemBox_iTEXT"><strong>SIZE:</strong> “”XL?? <em>(Extra Large??)</em></p>
<div id="sc_itemBox_img"><img src="<?php echo $row['imgThumb'];?>" /></div>
<p class=<p class="sc_itemBox_iTEXT"><strong>STYLE#</strong><?php echo $row['styleNUM']; ?> </p>
</div>
</td>
<?php } ?>
我认为让您的 $_SESSION['cart']
稍微复杂一点可能会有所帮助。尝试将您的 id
等分离到他们自己的数组中。也许是这样的。
addtocart.php
if(isset($_SESSION['cart'])) {
// Store just ids
// Use array_unique to filter this array
$_SESSION['cart']['id'] = array_unique($_SESSION['cart']['id']);
// Save new cart array with that items basics
$_SESSION['cart'][$_GET['id']]['size'][] = $_GET['size'];
$_SESSION['cart'][$_GET['id']]['qty'][] = $_GET['qty'];
header('Location:shoppingCart.php');
exit;
}
shoppingcart.php
// Implode just the id array, no other array in the cart session
// (make sure your addtocart checks for is_numeric so you don't get someone
// injecting sql junk into your db
$whereIn = implode(',', $_SESSION['cart']['id']);
$sql = "SELECT * FROM inventory WHERE id IN ($whereIn)";
编辑: 这里有一种方法可以让您将商品分成不同的尺寸(以防您添加多个具有相同 ID 但尺寸和数量不同的商品) :
function AddToCart()
{
// Confirm that an id is being added
// I am assuming there is an "add" trigger
if(isset($_GET['add']) && is_numeric($_GET['id'])) {
// Create the item in the cart
// Record size
if(isset($_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty']))
// Notice here that if there is already this item in the cart
// with the exact same size, it will sum
$_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty'] += $_GET['qty'];
else
// If not in cart at this size, it will add qty
$_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty'] = $_GET['qty'];
}
}
// Fetch ids for your query
function FetchItems()
{
if(isset($_SESSION['cart'])) {
foreach($_SESSION['cart'] as $itemcode => $array) {
$items[] = $itemcode;
}
return (isset($items))? $items:false;
}
}
// Start the session
session_start();
// Add to cart
AddToCart();
// This will fetch your ids for your query
$mysqlIds = implode(",",FetchItems());
echo '<pre>';
print_r($mysqlIds);
echo '</pre>'; ?>
<!-- These are just for testing. Will generate different sizes and qty-->
<a href="?add=true&id=1&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 1</a>
<a href="?add=true&id=2&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 2</a>
<a href="?add=true&id=3&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 3</a>
会给你:
// Session array after adding items to it.
Array
(
[cart] => Array
(
[2] => Array
(
[size] => Array
(
[21] => Array
(
[qty] => 1
)
[9] => Array
(
[qty] => 2
)
[8] => Array
(
[qty] => 0
)
[7] => Array
(
[qty] => 20
)
[2] => Array
(
[qty] => 5
)
)
)
[3] => Array
(
[size] => Array
(
[9] => Array
(
[qty] => 3
)
[1] => Array
(
[qty] => 0
)
[7] => Array
(
[qty] => 4
)
[10] => Array
(
[qty] => 6
)
[3] => Array
(
[qty] => 20
)
[2] => Array
(
[qty] => 10
)
[12] => Array
(
[qty] => 2
)
[6] => Array
(
[qty] => 10
)
)
)
[1] => Array
(
[size] => Array
(
[11] => Array
(
[qty] => 1
)
[3] => Array
(
[qty] => 3
)
[2] => Array
(
[qty] => 2
)
)
)
)
)
// This is for the ids for your mysql query
2,3,1