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;$iproductList);$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">

Insert title here

href=''>查看购物车

商品编号商品名称价格数量购买
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">

require '';

session_start();

$shopcar=unserialize($_SESSION['shopcar']);

print_r($shopcar);

$productList=$shopcar->productList;

foreach ($productList as $product){

?>

}?>

商品编号商品名称价格数量
1

name='num'

type='text'

value='

$product['num']?>' />


本文标签: 商品 购物车 方式 地址 传送