使用cookie和session,一下子搞定
解决方案 »
- curl多线程采集不是采集不到就是采不全?
- 字符串名称如何转为php变量名?
- 求助,PHP访问MySQL时出现httpd内存错误
- PHP无限分类的代码谁有???
- 哪位高手有删除代码注释的工具或程序啊?
- 想做PHP方面的兼职工作...100分,帮忙顶一下哦
- 合适的时候俺们聚会一下,互通有无。
- 讲高手指点
- PHP前途该如何发展?如今php似乎越来越斗不过asp.net了,做为一名php的开发者真有点痛心疾首啊,不知道该如何让php发展能够成为cgi开发中的有一次突破,再度担当黑马。。
- apache 配置 登录页面https 其他页面http
- 关于邮件函数的问题
- 一个入门级问题!烦请大家走过路过不要错过!
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();