使用cookie和session,一下子搞定
解决方案 »
- Parse error: syntax error, unexpected T_DO, expecting ';' in
- 【求助】php新手问题,用zend studio连接Oracle出错。
- 怎么在PHP网页中加入上一篇的链接
- PHP不能连接MYSQL数据库
- 论坛的讨论区怎么做?大家帮帮忙阿,不然就死了
- 一个奇怪的session问题,向大家分享一下
- 新的一年,老的问题. 强烈要求各位高手前来帮忙,并祝大家新年快乐,万事如意
- 类型为button的按钮,onclick后这样写为什么错了呢?
- 谁知道不用刷屏的聊天时怎么做?
- 关于session_onEnd()的问题
- 关于邮件函数的问题
- 一个入门级问题!烦请大家走过路过不要错过!
class Cart {
var $classname = "Cart";
var $persistent_slots = array("item", "currentItem"); var $item = array(); ## The shopping cart array($item, array("art"=>..., "num"=>..., ...))
var $currentItem = 1; ## Next item number in cart
##
## Return the position and number of units
## of an article in the cart(or false and 0,
## if it is not in there)
##
function check($art) { if (!is_array($this->item))
return array(false, 0);//考虑在用了unset的情况 reset($this->item);
while(list($item, $attr) = each($this->item)) {
if (isset($attr["art"])
&& ($attr["art"] == $art)) {
return array($item, $attr["num"]);
}
}
return array(false, 0);
} ##
## Delete all articles from current cart
##
function reset() { reset($this->item);
while(list($item, $attr) = each($this->item)) {
unset($this->item[$item]);
}
$this->currentItem = 1;
return true;
} ##
## Add num units of an article to the cart
## and return the item number (or false on error).
##
function add_item($art, $num) {
## Check to see if we already have some of these
list($item, $have) = $this->check($art);
## We already have them
if ($item) {
$this->item[$item]["num"] += $num;
return $item;
}
## New article
$item = $this->currentItem++;
$this->item[$item]["art"] = $art;
$this->item[$item]["num"] = $num; return $item;
}
##
## Take num units of an article from the cart
## and return the item number (or false on error)
##
function remove_item($art, $num) {
## Check to see if we have some of these
list($item, $have) = $this->check($art);
## Can't take them out
if (!$item || ($num > $have)) {
return false;
}
## Drop the item
if ($num == $have) {
unset($this->item[$item]);
return $item;
}
## Take $num out...
$this->item[$item]["num"] -= $num;
return $item;
} ##
## Set quantity of an article in the cart to exactly $num
## and return the item number
##
function set_item($art, $num) {
## Check to see if we already have some of these
list($item, $have) = $this->check($art);
## We already have them
if ($item) {
if ($num > 0) {
$this->item[$item]["num"] = $num;
} else {
unset($this->item[$item]);
}
return $item;
}
if ($num > 0) {
## New article
$item = $this->currentItem++;
$this->item[$item]["art"] = $art;
$this->item[$item]["num"] = $num;
} return $item;
} #
# Return the number of articles in current cart.
#
function num_items() {
if (!is_array($this->item))
return 0; return count($this->item);
} function tot_arts() {
printf("Please use \$cart->num_items() instead.\n"); ## Comment out, if you want.
return $this->num_items();
} #
# Iterator to show cart contents.
#
function show_all() {
if (!is_array($this->item) or $this->num_items() == 0) {
$this->show_empty_cart();
return false;
} reset($this->item);
$this->show_cart_open();
while(list($item, $attr) = each($this->item)) {
$this->show_item($attr["art"], $attr["num"]);
}
$this->show_cart_close();
} function start() {
global $sess;
$sess->register("cart");
} ##
## Dummy, to be overwritten by user.
## function show_cart_open() {
echo("aa");
print_r($this->item);
return;
} function show_cart_close() {
return;
} function show_item($art, $num) {
printf("%s units of %s<br>\n", $num, $art);
}
function show_empty_cart() {
printf("Your shopping cart is empty.<br>\n");
}
}//Test Local
$t = new cart;
echo($t->add_item("wogannima", 4));
echo($t->add_item("wogannima", 4));
echo($t->add_item("dada", 4));
$t->set_item("wogannima", 4);
$t->remove_item("wogannima", 5);
$t->show_all();
$t->show_cart_open();