Replacing/updating 会话数组
Replacing/updating SESSION arrays
这有点与我的第一个 post 有关,但是是新问题。我不确定协议,所以我创建了一个新主题。 (link to first post)
我在这里四处搜索,发现了我的问题的几个不同实例,但似乎无法理解任何足以推进我自己的程序的内容。
简要说明:购物车。我在带有 itemID 的页面上列出了一些项目。用户点击一个项目,它被添加到购物车。如果用户点击同一个项目两次,数量应该增加一个(目前是硬编码的)。 $_SESSION['cart'] 数组应该开始为空并且是动态的。
当前问题:在测试中,它似乎只循环遍历数组一次,array_push 给出错误,即使参数一是数组,也没有按应有的方式删除项目,也没有读取数量在for循环中。它在 print_r($_SESSION['cart']
中显示“1”
但是 $_SESSION['cart']['itemID']['quantity'];
中的 '0'
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\A06_DictCart\controller\updateCart.php on line 25
我想是一些不同的问题,但解决一个问题会帮助我继续处理其他问题,我认为它们是半相关的。
viewCart.php
<?php
session_start();
/* Display the itemID and quantity of each item in the shopping cart.
This may be implemented by iterating though the items in the dictionary
and displaying their keys and values. */
echo ' ** array/ '.print_r($_SESSION['cart']).' ** count/ '.count($_SESSION['cart']);
echo '<br>'.$_SESSION['cart']['itemID']['quantity'];
echo '<center><h2><u>SHOPPING CART</u></h2>';
echo '<br><TABLE border=1 cellPadding=3 cellSpacing=1>
<TBODY>
<TR>
<TD>ItemID</TD>
<TD>Quantity</TD>
<TD>Remove from Cart</TD></TR>';
if (isset($_SESSION['cart'])) {
$c = count($_SESSION['cart']);
for ($x=0; $x <= $c; $x++){
echo '<tr><td>'.$_SESSION['cart']['itemID'].'</td>';
echo '<td>'.$_SESSION['cart']['itemID']['quantity'].'</td>';
echo "<td><a href='updateCart.php?action=remove&itemID=".$_SESSION['cart']['itemID']."&quantity=".$qty."> Remove from Cart </a></td></tr>";
$x++;
}
}
else {
$msg = '<i> ** cart is empty ** </i>';
echo '<tr> <td></td>';
echo '<td> </td>';
echo "<td> </td></tr>";
}
echo '</tbody></table>';
echo $msg;
echo '<br><br><a href="../controller/default.php">Back to Catalog</a>';
echo '<br><a href="../controller/updateCart.php?action=clear">Empty Cart</a></center>';
?>
updateCart.php
<?php
/* Read the values of action, itemID, and quantity from the querystring.
Items will either be added or removed from the shopping cart depending
on the values of the querystring. Once the quantity is 0 the item should
be unset from the cart. */
session_start();
$action = $_GET['action'];
$itemID = $_GET['itemID'];
$qty = $_GET['quantity'];
$msg;
if (empty($_SESSION['cart'])) $_SESSION['cart'] = array();
//array(); //('123' => 0, '456' => 0, '789' => 0, '101' => 0);
//$cart = array($_SESSION['cart']);
// ADD TO CART
if ($action == 'add') {
if (!in_array($itemID, $_SESSION['cart'])) {
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$temp = array($_SESSION['cart']);
$q += $_SESSION['cart'][$itemID]['qty'];
$temp = array('itemID' => $itemID, 'quantity' => $q);
array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
}
$msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
// REMOVE FROM CART
if ($action == 'remove'){
if (($_SESSION['cart'][$itemID]['quantity'] - 1) <= 0)
unset ($_SESSION['cart'][$itemID]); // If new value is zero, unset elements
else
$_SESSION['cart'][$itemID]['quantity']--; // Else decrease quantity by one
$msg = $qty.' of item # '.$itemID.' has been removed to your cart.';
}
// EMPTY CART
if ($action == 'clear') {
unset($_SESSION['cart']); // Unset session
session_destroy();
$msg = 'Your shopping cart has been emptied.';
}
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<center>
<p> <?php echo $msg; ?><br><br>
<p><a href="../controller/default.php">Back to Catalog</a></p>
<p><a href="../controller/viewCart.php">View Cart</a></p>
</center>
</BODY>
</HTML>
由于这是一项正在进行的工作,因此我尚未发现代码语法方面的一些差异。我在另一个post.
中看到的'remove from cart'语句中的一些代码
感谢任何帮助。
我没有测试这个,但是试试这个代码:
// ADD TO CART
if ($action == 'add') {
if (!isset($_SESSION['cart'][$itemID])) {
$_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$_SESSION['cart'][$itemID]['quantity'] += $qty
}
$msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
我就是这样写的。我想在你的代码中
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
应该是
$_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
而且我认为您在原始代码中混淆了 'qty' 和 'quantity' 键。在更新时,您阅读了密钥 qty
但您的初始化程序将 quantity
作为密钥:
$q += $_SESSION['cart'][$itemID]['qty'];
可能应该写成
$q += $_SESSION['cart'][$itemID]['quantity'];
编辑:注意这个
在开始编写任何代码之前,您可能需要考虑应用的逻辑以及您要实现的目标。
if ($action == 'add') {
if (!in_array($itemID, $_SESSION['cart'])) {
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$temp = array($_SESSION['cart']);
$q += $_SESSION['cart'][$itemID]['qty'];
$temp = array('itemID' => $itemID, 'quantity' => $q);
array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
}
}
应该只是:
if ($action == 'add' ) {
$_SESSION['cart'][$itemID]+=$qty;
}
(注意数组结构的变化)
这有点与我的第一个 post 有关,但是是新问题。我不确定协议,所以我创建了一个新主题。 (link to first post)
我在这里四处搜索,发现了我的问题的几个不同实例,但似乎无法理解任何足以推进我自己的程序的内容。
简要说明:购物车。我在带有 itemID 的页面上列出了一些项目。用户点击一个项目,它被添加到购物车。如果用户点击同一个项目两次,数量应该增加一个(目前是硬编码的)。 $_SESSION['cart'] 数组应该开始为空并且是动态的。
当前问题:在测试中,它似乎只循环遍历数组一次,array_push 给出错误,即使参数一是数组,也没有按应有的方式删除项目,也没有读取数量在for循环中。它在 print_r($_SESSION['cart']
中显示“1”
但是 $_SESSION['cart']['itemID']['quantity'];
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\A06_DictCart\controller\updateCart.php on line 25
我想是一些不同的问题,但解决一个问题会帮助我继续处理其他问题,我认为它们是半相关的。
viewCart.php
<?php
session_start();
/* Display the itemID and quantity of each item in the shopping cart.
This may be implemented by iterating though the items in the dictionary
and displaying their keys and values. */
echo ' ** array/ '.print_r($_SESSION['cart']).' ** count/ '.count($_SESSION['cart']);
echo '<br>'.$_SESSION['cart']['itemID']['quantity'];
echo '<center><h2><u>SHOPPING CART</u></h2>';
echo '<br><TABLE border=1 cellPadding=3 cellSpacing=1>
<TBODY>
<TR>
<TD>ItemID</TD>
<TD>Quantity</TD>
<TD>Remove from Cart</TD></TR>';
if (isset($_SESSION['cart'])) {
$c = count($_SESSION['cart']);
for ($x=0; $x <= $c; $x++){
echo '<tr><td>'.$_SESSION['cart']['itemID'].'</td>';
echo '<td>'.$_SESSION['cart']['itemID']['quantity'].'</td>';
echo "<td><a href='updateCart.php?action=remove&itemID=".$_SESSION['cart']['itemID']."&quantity=".$qty."> Remove from Cart </a></td></tr>";
$x++;
}
}
else {
$msg = '<i> ** cart is empty ** </i>';
echo '<tr> <td></td>';
echo '<td> </td>';
echo "<td> </td></tr>";
}
echo '</tbody></table>';
echo $msg;
echo '<br><br><a href="../controller/default.php">Back to Catalog</a>';
echo '<br><a href="../controller/updateCart.php?action=clear">Empty Cart</a></center>';
?>
updateCart.php
<?php
/* Read the values of action, itemID, and quantity from the querystring.
Items will either be added or removed from the shopping cart depending
on the values of the querystring. Once the quantity is 0 the item should
be unset from the cart. */
session_start();
$action = $_GET['action'];
$itemID = $_GET['itemID'];
$qty = $_GET['quantity'];
$msg;
if (empty($_SESSION['cart'])) $_SESSION['cart'] = array();
//array(); //('123' => 0, '456' => 0, '789' => 0, '101' => 0);
//$cart = array($_SESSION['cart']);
// ADD TO CART
if ($action == 'add') {
if (!in_array($itemID, $_SESSION['cart'])) {
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$temp = array($_SESSION['cart']);
$q += $_SESSION['cart'][$itemID]['qty'];
$temp = array('itemID' => $itemID, 'quantity' => $q);
array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
}
$msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
// REMOVE FROM CART
if ($action == 'remove'){
if (($_SESSION['cart'][$itemID]['quantity'] - 1) <= 0)
unset ($_SESSION['cart'][$itemID]); // If new value is zero, unset elements
else
$_SESSION['cart'][$itemID]['quantity']--; // Else decrease quantity by one
$msg = $qty.' of item # '.$itemID.' has been removed to your cart.';
}
// EMPTY CART
if ($action == 'clear') {
unset($_SESSION['cart']); // Unset session
session_destroy();
$msg = 'Your shopping cart has been emptied.';
}
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<center>
<p> <?php echo $msg; ?><br><br>
<p><a href="../controller/default.php">Back to Catalog</a></p>
<p><a href="../controller/viewCart.php">View Cart</a></p>
</center>
</BODY>
</HTML>
由于这是一项正在进行的工作,因此我尚未发现代码语法方面的一些差异。我在另一个post.
中看到的'remove from cart'语句中的一些代码感谢任何帮助。
我没有测试这个,但是试试这个代码:
// ADD TO CART
if ($action == 'add') {
if (!isset($_SESSION['cart'][$itemID])) {
$_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$_SESSION['cart'][$itemID]['quantity'] += $qty
}
$msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
我就是这样写的。我想在你的代码中
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
应该是
$_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
而且我认为您在原始代码中混淆了 'qty' 和 'quantity' 键。在更新时,您阅读了密钥 qty
但您的初始化程序将 quantity
作为密钥:
$q += $_SESSION['cart'][$itemID]['qty'];
可能应该写成
$q += $_SESSION['cart'][$itemID]['quantity'];
编辑:注意这个
在开始编写任何代码之前,您可能需要考虑应用的逻辑以及您要实现的目标。
if ($action == 'add') {
if (!in_array($itemID, $_SESSION['cart'])) {
$_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty); // Insert new item
} else {
$temp = array($_SESSION['cart']);
$q += $_SESSION['cart'][$itemID]['qty'];
$temp = array('itemID' => $itemID, 'quantity' => $q);
array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
}
}
应该只是:
if ($action == 'add' ) {
$_SESSION['cart'][$itemID]+=$qty;
}
(注意数组结构的变化)