国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php購物車實現代碼

2024-05-04 23:16:52
字體:
來源:轉載
供稿:網友
ShopCar.php

復制代碼 代碼如下:


<?php
class Shopcar
{
//商品列表
public $productList=array();
/**
*
* @param unknown_type $product 傳進來的商品
* @return true 購物車里面沒有該商品
*/
public function checkProduct($product)
{
for($i=0;$i<count($this->productList);$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;
}
}


productList.html

復制代碼 代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src='jquery.min.js'></script>
<script type="text/javascript">
function buy(i)
{
var num=$(':input[name=num]')[i].value;
var name=$('[name=name]')[i].innerHTML;
var price=$('[name=price]')[i].innerHTML;
alert(num+name+price);
$.ajax({
type:'post', //傳送的方式,get/post
url:'index.php', //發送數據的地址
cache:'false',
data:'num='+num+"&name="+name+"&price="+price,
success:function(data)
{
alert(data);
}
})
}
</script>
</head>
<body>
<table>
<tr><td>商品編號</td><td>商品名稱</td><td>價格</td><td>數量</td><td>購買</td></tr>
<tr><td>0</td><td><label >商品1</label></td><td><label>1</label>
</td><td><input type='text' value='1' /></td><td><a><u><font color='blue'>購買</font></u></a></td></tr>
<tr><td>1</td><td><label >商品2</label></td><td><label>2</label>
</td><td><input type='text' value='1' /></td><td><a>購買</a></td></tr>
<tr><td>2</td><td><label >商品3</label></td><td><label>1</label>
</td><td><input type='text' value='1' /></td><td><a>購買</a></td></tr>
<tr><td>3</td><td><label >商品4</label></td><td><label>1</label>
</td><td><input type='text' value='1' /></td><td><a>購買</a></td></tr>
<tr><a href='show.php'>查看購物車</a></tr>
</table>
</body>
</html>


index.php

復制代碼 代碼如下:


<?php
require 'Shopcar.class.php';
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);


show.php

復制代碼 代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table>
<tr><td>商品編號</td><td>商品名稱</td><td>價格</td><td>數量</td></tr>
<?php
require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);
print_r($shopcar);
$productList=$shopcar->productList;
foreach ($productList as $product){
?>
<tr><td>1</td><td><label ><?php echo $product['name']?></label></td><td><label><?php echo $product['price']?></label>
</td><td><input type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>
</body>
</html>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐乡市| 庄河市| 竹北市| 景宁| 正宁县| 大竹县| 宁强县| 讷河市| 玛沁县| 开封市| 台中市| 芜湖县| 封丘县| 安福县| 泌阳县| 林州市| 乐东| 郑州市| 台东县| 留坝县| 丰镇市| 宁明县| 麟游县| 林口县| 横峰县| 潢川县| 襄城县| 贞丰县| 平泉县| 宜黄县| 游戏| 长子县| 潞西市| 麻城市| 南开区| 松溪县| 汝南县| 资中县| 余庆县| 石楼县| 广西|