admin 管理员组文章数量: 1184232
2024年3月8日发(作者:plsql instr函数)
php购物车实现
?
class
Shopcar
{
//商品列表
public
$productList=array();
/**
*
* @param unknown_type $product 传进来的商品
* @return true 购物车里面没有该商品
*/
public
function
checkProduct($product)
{
for($i=0;$i
{
if($this->productList[$i]['name']==$product['name'])
return
$i;
}
return
-1;
}
//添加到购物车
public
function
add($product)
{
$i=$this->checkProduct($product);
if($i==-1)
array_push($this->productList,$product);
else
$this->productList[$i]['num']+=$product['num'];
}
//删除
public
function
delete($product)
{
$i=$this->checkProduct($product);
if($i!=-1)
array_splice($this->productList,$i,1);
}
//返回所有的商品的信息
public
function
show()
{
return
$this->productList;
}
}
?
"/TR/html4/">
http-equiv="Content-Type"
content="text/html; charset=UTF-8">
| 商品编号 | 商品名称 | 价格 | 数量 | 购买 |
| 0 |
| name='num' type='text' value='1' /> | onclick='buy(0)'> color='blue'>购买 | |
| 1 |
| name='num' type='text' value='1' /> | onclick='buy(1)'>购买 | |
| 2 |
| name='num' type='text' value='1' /> | onclick='buy(2)'>购买 | |
| 3 |
| name='num' type='text' value='1' /> | onclick='buy(3)'>购买 | |
?
require
'';
session_start();
$name=$_POST['name'];
$num=$_POST['num'];
$price=$_POST['price'];
$product=array('name'=>$name,'num'=>$num,'price'=>$price);
print_r($product);
if(isset($_SESSION['shopcar']))
$shopcar=unserialize($_SESSION['shopcar']);
else
$shopcar=new
Shopcar();
$shopcar->add($product);
$_SESSION['shopcar']=serialize($shopcar);
?
"/TR/html4/">
http-equiv="Content-Type"
content="text/html; charset=UTF-8">
| 商品编号 | 商品名称 | 价格 | 数量 |
| 1 |
| name='num' type='text' value=' $product['num']?>' /> |
版权声明:本文标题:php购物车源码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1709890860a548917.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论