【发布时间】:2015-10-03 21:00:08
【问题描述】:
这有点与我的第一篇文章有关,但是是新问题。我不确定协议,所以我创建了一个新主题。 (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>
由于这是一项正在进行中的工作,因此在代码语法方面可能存在一些我尚未发现的差异。我在另一篇文章中看到的“从购物车中删除”语句中的一些代码。
感谢任何帮助。
【问题讨论】:
标签: php arrays session cart associative