- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章PHP购物车类Cart.class.php定义与用法示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下:
之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加什么的都使用JS),但是浏览器兼容不好, 今天终于出问题了, 有个老外购物了产品, 由于使用了不知名的浏览器, chrome, opera…都有可能, 因此, 我多了一份工作, 重写购物车. 。
不打算再使用JS, 直接考虑php. 。
找到了一个购物车的类, 使用起来很方便. 。
Cart.class.php源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
<?php
/**
* Cart
*
* 购物车类
*
* @author doodoo<pwtitle @yahoo.com.cn="">
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @version $Revision: 1.10 $
*/
Class Cart{
var
$cart
;
var
$totalCount
;
//商品总数量
var
$totalPrices
;
//商品总金额
/**
* Cart Constructor
*
* 类的构造函数,使购物车保持稳定的初始化状态
*
* @static
* @access public
* @return void 无返回值
* @param void 无参数
*/
function
Cart(){
$this
->totalCount = 0;
$this
->totalPrice = 0;
$this
->cart =
array
();
}
// }}}
// {{{ add($item)
/**
* 增加商品到当前购物车
*
* @access public
* @param array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
* @return array 返回当前购物车内商品的数组
*/
function
add(
$item
){
if
(!
is_array
(
$item
)||
is_null
(
$item
))
return
$this
->cart;
if
(!
is_numeric
(
end
(
$item
))||(!
is_numeric
(prev(
$item
)))) {
echo
"价格和数量必须是数字"
;
return
$this
->cart;
}
reset(
$item
);
//这一句是必须的,因为上面的判断已经移动了数组的指标
$key
= current(
$item
);
if
(
$key
==
""
)
return
$this
->cart;
if
(
$this
->_isExists(
$key
)){
//商品是否已经存在?
$this
->cart[
$key
][
'count'
] =
end
(
$item
);
return
$this
->cart;
}
$this
->cart[
$key
][
'ID'
] =
$key
;
$this
->cart[
$key
][
'name'
] = next(
$item
);
$this
->cart[
$key
][
'price'
] = next(
$item
);
$this
->cart[
$key
][
'count'
] = next(
$item
);
return
$this
->cart;
}
// }}}
// {{{ add($item)
/**
* 从当前购物车中取出部分或全部商品
* 当 $key=="" 的时候,清空当前购物车
* 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
* 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
*
* @access public
* @param string $key 商品ID
* @return mixed 返回真假或当前购物车内商品的数组
*/
function
remove(
$key
=
""
,
$count
=
""
){
if
(
$key
==
""
) {
$this
->cart =
array
();
return
true;
}
if
(!
array_key_exists
(
$key
,
$this
->cart))
return
false;
if
(
$count
==
""
){
//移去这一类商品
unset(
$this
->cart[
$key
]);
}
else
{
//移去$count个商品
$this
->cart[
$key
][
'count'
] -=
$count
;
if
(
$this
->cart[
$key
][
'count'
]<=0) unset(
$this
->cart[
$key
]);
}
return
$this
->cart;
}
// }}}
// {{{ modi($key,$value)
/**
* 修改购物车内商品ID为 $key 的商品的数量为 $value
*
* @access public
* @param string $key 商品ID
* @param int $value 商品数量
* @return array 返回当前购物车内商品的数组;
*/
function
modi(
$key
,
$value
){
if
(!
$this
->_isExists(
$key
))
return
$this
->cart();
//不存在此商品,直接返回
if
(
$value
<=0){
// value 太小,全部删除
unset(
$this
->cart[
$key
]);
return
$this
->cart;
}
$this
->cart[
$key
][
'count'
] =
$value
;
return
$this
->cart;
}
/**
* 返回当前购物车内商品的数组
*
* @access public
* @return array 返回当前购物车内商品的数组;
*/
function
getCart(){
return
$this
->cart;
}
// }}}
// {{{ _isExists($key)
/**
* 判断当前购物车中是否存在商品ID号为$key的商品
*
* @access private
* @param string $key 商品ID
* @return bool true or false;
*/
function
_isExists(
$key
)
{
if
(isset(
$this
->cart[
$key
])&&!
empty
(
$this
->cart[
$key
])&&
array_key_exists
(
$key
,
$this
->cart))
return
true;
return
false;
}
// }}}
// {{{ isEmpty()
/**
* 判断当前购物车是否为空,即没有任何商品
*
* @access public
* @return bool true or false;
*/
function
isEmpty(){
return
!
count
(
$this
->cart);
}
// }}}
// {{{ _stat()
/**
* 取得部分统计信息
*
* @access private
* @return bool true or false;
*/
function
_stat(){
if
(
$this
->isEmpty())
return
false;
foreach
(
$this
->cart
as
$item
){
$this
->totalCount = @
end
(
$item
);
$this
->totalPrices = @prev(
$item
);
}
return
true;
}
// }}}
// {{{ totalPrices()
/**
* 取得当前购物车所有商品的总金额
*
* @access public
* @return float 返回金额;
*/
function
totalPrices(){
if
(
$this
->_stat())
return
$this
->totalPrices;
return
0;
}
// }}}
// {{{ isEmpty()
/**
* 取得当前购物车所有商品的总数量和
*
* @access public
* @return int ;
*/
function
totalCount(){
if
(
$this
->_stat())
return
$this
->totalCount;
return
0;
}
}
//End Class Cart
?>
|
使用该类的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?php
header(
"Content-type:text/html;charset=utf8"
);
//调用实例
require_once
'Cart.class.php'
;
session_start();
if
(!isset(
$_SESSION
[
'cart'
])) {
$_SESSION
[
'cart'
] =
new
Cart;
}
$cart
=&
$_SESSION
[
'cart'
];
if
( (
$_SERVER
[
'REQUEST_METHOD'
]==
"POST"
)&&(
$_POST
[
'action'
]==
'add'
) ){
$p
=
$_POST
[
'p'
];
$items
=
$cart
->add(
$p
);
}
if
( (
$_GET
[
'action'
]==
'remove'
)&&(
$_GET
[
'key'
]!=
""
) ) {
$items
=
$cart
->remove(
$_GET
[
'key'
]);
}
if
( (
$_SERVER
[
'REQUEST_METHOD'
]==
"POST"
)&&(
$_POST
[
'action'
]==
'modi'
) ){
$key
=
$_POST
[
'key'
];
$value
=
$_POST
[
'value'
];
for
(
$i
=0;
$i
<
count
(
$key
);
$i
=
""
$items
=
"$cart-"
){=
""
>modi(
$key
[
$i
],
$value
[
$i
]);
}
}
$items
=
$cart
->getCart();
//打印
echo
""
;
setlocale(LC_MONETARY,
'it_IT'
);
foreach
(
$items
as
$item
){
echo
""
;
echo
"<table border="
1
"><tbody><tr><form action="
\"index.php\"
" method="
\
" post\??="
"></form><td>ID:"
.
$item
[
'ID'
].
"<input type="
hidden
" value="
.
$item
[
'ID'
].
" name="
key[]
">"
;
echo
"</td><td>产品:"
.
$item
[
'name'
];
echo
"</td><td>单价:"
.
$item
[
'price'
];
echo
"</td><td><input value="
.
$item
[
'count'
].
" name="
value[]
">"
;
$sum
=
$item
[
'count'
]*
$item
[
'price'
];
echo
"</td><td>合计:"
.
round
(
$sum
,2);
echo
"</td><td><input onclick="
\"location=
'?action=remove&key=".$item['
ID
']."'
\"
" type="
button
" value="
删除
">"
; }
echo
"<input type="
hidden
" value="
modi
" name="
action
">"
;
echo
"</td></tr><tr><td colspan="
7
"><input type="
submit
" value="
提交查询内容
">"
;
echo
"</td></tr></tbody></table>"
;
?>
<hr>
<form action=
"tmp.php"
method=
"post"
>
ID:<input name=
"p[]"
>
品名:<input name=
"p[]"
>
单价:<input name=
"p[]"
>
数量:<input name=
"p[]"
>
<input type=
"hidden"
value=
"add"
name=
"action"
>
<input type=
"submit"
value=
"提交查询内容"
>
</form></
count
>
|
希望本文所述对大家PHP程序设计有所帮助.
最后此篇关于PHP购物车类Cart.class.php定义与用法示例的文章就讲到这里了,如果你想了解更多关于PHP购物车类Cart.class.php定义与用法示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Sample data for IPv6? 除了 wireshark 在其网站上提供的内容之外,是否有可以下
我正在寻找可以集成到现有应用程序中并使用多拖放功能的示例或任何现成的解决方案。我在互联网上找到的大多数解决方案在将多个项目从 ListBox 等控件拖放到另一个 ListBox 时效果不佳。谁能指出我
我是 GATE Embedded 的新手,我尝试了简单的示例并得到了 NoClassDefFoundError。首先我会解释我尝试了什么 在 D:\project\gate-7.0 中下载并提取 Ga
是否有像 Eclipse 中的 SWT 示例那样的多合一 JFace 控件示例?搜索(在 stackoverflow.com 上使用谷歌搜索和搜索)对我没有帮助。 如果它是一个独立的应用程序或 ecl
我找不到任何可以清楚地解释如何通过 .net API(特别是 c#)使用谷歌计算引擎的内容。有没有人可以指点我什么? 附言我知道 API 引用 ( https://developers.google.
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
我正在尝试为我的应用程序设计配置文件格式并选择了 YAML。但是,这(显然)意味着我需要能够定义、解析和验证正确的 YAML 语法! 在配置文件中,必须有一个名为 widgets 的集合/序列。 .这
你能给我一个使用 pysmb 库连接到一些 samba 服务器的例子吗?我读过有类 smb.SMBConnection.SMBConnection(用户名、密码、my_name、remote_name
linux服务器默认通过22端口用ssh协议登录,这种不安全。今天想做限制,即允许部分来源ip连接服务器。 案例目标:通过iptables规则限制对linux服务器的登录。 处理方法:编
我一直在寻找任何 PostProjectAnalysisTask 工作代码示例,但没有看。 This页面指出 HipChat plugin使用这个钩子(Hook),但在我看来它仍然使用遗留的 Po
我发现了 GWT 的 CustomScrollPanel 以及如何自定义滚动条,但我找不到任何示例或如何设置它。是否有任何示例显示正在使用的自定义滚动条? 最佳答案 这是自定义 native 滚动条的
我正在尝试开发一个 Backbone Marionette 应用程序,我需要知道如何以最佳方式执行 CRUD(创建、读取、更新和销毁)操作。我找不到任何解释这一点的资源(仅适用于 Backbone)。
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 去年关闭。 Improve this
我需要一个提交多个单独请求的 django 表单,如果没有大量定制,我找不到如何做到这一点的示例。即,假设有一个汽车维修店使用的表格。该表格将列出商店能够进行的所有可能的维修,并且用户将选择他们想要进
我有一个 Multi-Tenancy 应用程序。然而,这个相同的应用程序有 liquibase。我需要在我的所有数据源中运行 liquibase,但是我不能使用这个 Bean。 我的应用程序.yml
我了解有关单元测试的一般思想,并已在系统中发生复杂交互的场景中使用它,但我仍然对所有这些原则结合在一起有疑问。 我们被警告不要测试框架或数据库。好的 UI 设计不适合非人工测试。 MVC 框架不包括一
我正在使用 docjure并且它的 select-columns 函数需要一个列映射。我想获取所有列而无需手动指定。 如何将以下内容生成为惰性无限向量序列 [:A :B :C :D :E ... :A
$condition使用说明和 $param在 findByAttributes在 Yii 在大多数情况下,这就是我使用 findByAttributes 的方式 Person::model()->f
我在 Ubuntu 11.10 上安装了 qtcreator sudo apt-get install qtcreator 安装的版本有:QT Creator 2.2.1、QT 4.7.3 当我启动
我是一名优秀的程序员,十分优秀!