使用面向对象PHP的购物车
我最近在一个OO购物车的网上发现了一些代码。对于购物车中已存在的项目,结果如下所示 购物车内容(2项) 一些小部件:每件1美元23.45美元 另一个小部件:每个4美元12.39美元 我的目标是创建一个如下的产品列表,允许用户自己在购物车中添加/删除物品(如上所述),但使用按钮并允许用户自己更改数量。有谁能帮我实现这一点吗?我还听说有人在购物车中使用会话?感谢您的帮助,谢谢 产品列表 一些小部件:每个23.45美元。数量(1)添加到购物车 另一个小部件:每个12.39美元。数量(1)添加到购物车 购物车 另一个小部件:12.39美元。数量(4)移除 ShoppingCart.php使用面向对象PHP的购物车,php,arrays,class,oop,object,Php,Arrays,Class,Oop,Object,我最近在一个OO购物车的网上发现了一些代码。对于购物车中已存在的项目,结果如下所示 购物车内容(2项) 一些小部件:每件1美元23.45美元 另一个小部件:每个4美元12.39美元 我的目标是创建一个如下的产品列表,允许用户自己在购物车中添加/删除物品(如上所述),但使用按钮并允许用户自己更改数量。有谁能帮我实现这一点吗?我还听说有人在购物车中使用会话?感谢您的帮助,谢谢 产品列表 一些小部件:每个23.45美元。数量(1)添加到购物车 另一个小部件:每个12.39美元。数量(1)添加到购物车
<?php
class ShoppingCart implements Iterator, Countable {
// Array stores the list of items in the cart:
protected $items = array();
// For tracking iterations:
protected $position = 0;
// For storing the IDs, as a convenience:
protected $ids = array();
// Constructor just sets the object up for usage:
function __construct() {
$this->items = array();
$this->ids = array();
}
// Returns a Boolean indicating if the cart is empty:
public function isEmpty() {
return (empty($this->items));
}
// Adds a new item to the cart:
public function addItem(Item $item) {
// Need the item id:
$id = $item->getId();
// Throw an exception if there's no id:
if (!$id) throw new Exception('The cart requires items with unique ID
values.');
// Add or update:
if (isset($this->items[$id])) {
$this->updateItem($item, $this->items[$item]['qty'] + 1);
} else {
$this->items[$id] = array('item' => $item, 'qty' => 1);
$this->ids[] = $id; // Store the id, too!
}
} // End of addItem() method.
// Changes an item already in the cart:
public function updateItem(Item $item, $qty) {
// Need the unique item id:
$id = $item->getId();
// Delete or update accordingly:
if ($qty === 0) {
$this->deleteItem($item);
} elseif ( ($qty > 0) && ($qty != $this->items[$id]['qty'])) {
$this->items[$id]['qty'] = $qty;
}
} // End of updateItem() method.
// Removes an item from the cart:
public function deleteItem(Item $item) {
// Need the unique item id:
$id = $item->getId();
// Remove it:
if (isset($this->items[$id])) {
unset($this->items[$id]);
// Remove the stored id, too:
$index = array_search($id, $this->ids);
unset($this->ids[$index]);
// Recreate that array to prevent holes:
$this->ids = array_values($this->ids);
}
} // End of deleteItem() method.
// Required by Iterator; returns the current value:
public function current() {
// Get the index for the current position:
$index = $this->ids[$this->position];
// Return the item:
return $this->items[$index];
} // End of current() method.
// Required by Iterator; returns the current key:
public function key() {
return $this->position;
}
// Required by Iterator; increments the position:
public function next() {
$this->position++;
}
// Required by Iterator; returns the position to the first spot:
public function rewind() {
$this->position = 0;
}
public function valid() {
return (isset($this->ids[$this->position]));
}
// Required by Countable:
public function count() {
return count($this->items);
}
} // End of ShoppingCart class.
?>
你的课程学得很好。现在您只需编辑“模板”或您的cart.php
如果要打印所有项目,您必须使用“添加到购物车”按钮打印每个项目(在html中,您可以使用“添加到购物车”按钮为每个项目制作一个表单,传递项目的金额和ID。
要查看购物车中的内容,请在cart.php中执行操作
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the item:
printf('<p><strong>%s</strong>: %d @ $%0.2f each.<p>', $item->getName(),
$arr['qty'], $item->getPrice());
} // End of foreach loop!
我输入的函数名是printItem,以避免保留字(仅“print”可能很危险)
然后,在模板(表)中可以替换
echo $w1 by echo $w1->printItem() //Or something like that
我希望这将对您有所帮助。=)我应该为此创建一个新页面吗?你能给我一些示例代码吗?对不起,我对OO@karramarrogorri很陌生,我已经更新了这个问题。。你能看一下吗@Karramarrogorria另一件需要注意的事情是,您不能在浏览器中编辑服务器端变量。你想做一个网页吗?
<table>
<tr><td><?php echo $w1; ?></td><td><button onclick="$cart-
>addItem($w1);">Add to cart</button></td></tr>
<tr><td><?php echo $w2; ?></td><td><button onclick="$cart-
>addItem($w2);">Add to cart</button></td></tr>
<tr><td><?php echo $w3; ?></td><td><button onclick="$cart-
>addItem($w3);">Add to cart</button></td></tr>
</table>
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the item:
printf('<p><strong>%s</strong>: %d @ $%0.2f each.<p>', $item->getName(),
$arr['qty'], $item->getPrice());
} // End of foreach loop!
public function printItem(){
return $this->name." ".$this->price //Or whatever you want to print.
}
echo $w1 by echo $w1->printItem() //Or something like that