php購(gòu)物車實(shí)現(xiàn)代碼
更新時(shí)間:2011年10月10日 23:33:59 作者:
php購(gòu)物車實(shí)現(xiàn)代碼這個(gè)代碼比較全,需要的朋友可以參考下。
ShopCar.php
<?php
class Shopcar
{
//商品列表
public $productList=array();
/**
*
* @param unknown_type $product 傳進(jìn)來(lái)的商品
* @return true 購(gòu)物車?yán)锩鏇](méi)有該商品
*/
public function checkProduct($product)
{
for($i=0;$i<count($this->productList);$i++ )
{
if($this->productList[$i]['name']==$product['name'])
return $i;
}
return -1;
}
//添加到購(gòu)物車
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', //發(fā)送數(shù)據(jù)的地址
cache:'false',
data:'num='+num+"&name="+name+"&price="+price,
success:function(data)
{
alert(data);
}
})
}
</script>
</head>
<body>
<table>
<tr><td>商品編號(hào)</td><td>商品名稱</td><td>價(jià)格</td><td>數(shù)量</td><td>購(gòu)買</td></tr>
<tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(0)'><u><font color='blue'>購(gòu)買</font></u></a></td></tr>
<tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(1)'>購(gòu)買</a></td></tr>
<tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(2)'>購(gòu)買</a></td></tr>
<tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(3)'>購(gòu)買</a></td></tr>
<tr><a href='show.php'>查看購(gòu)物車</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>商品編號(hào)</td><td>商品名稱</td><td>價(jià)格</td><td>數(shù)量</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 name='price'><?php echo $product['price']?></label>
</td><td><input name='num' type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>
</body>
</html>
復(fù)制代碼 代碼如下:
<?php
class Shopcar
{
//商品列表
public $productList=array();
/**
*
* @param unknown_type $product 傳進(jìn)來(lái)的商品
* @return true 購(gòu)物車?yán)锩鏇](méi)有該商品
*/
public function checkProduct($product)
{
for($i=0;$i<count($this->productList);$i++ )
{
if($this->productList[$i]['name']==$product['name'])
return $i;
}
return -1;
}
//添加到購(gòu)物車
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
復(fù)制代碼 代碼如下:
<!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', //發(fā)送數(shù)據(jù)的地址
cache:'false',
data:'num='+num+"&name="+name+"&price="+price,
success:function(data)
{
alert(data);
}
})
}
</script>
</head>
<body>
<table>
<tr><td>商品編號(hào)</td><td>商品名稱</td><td>價(jià)格</td><td>數(shù)量</td><td>購(gòu)買</td></tr>
<tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(0)'><u><font color='blue'>購(gòu)買</font></u></a></td></tr>
<tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(1)'>購(gòu)買</a></td></tr>
<tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(2)'>購(gòu)買</a></td></tr>
<tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(3)'>購(gòu)買</a></td></tr>
<tr><a href='show.php'>查看購(gòu)物車</a></tr>
</table>
</body>
</html>
index.php
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<!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>商品編號(hào)</td><td>商品名稱</td><td>價(jià)格</td><td>數(shù)量</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 name='price'><?php echo $product['price']?></label>
</td><td><input name='num' type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>
</body>
</html>
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)購(gòu)物車多物品數(shù)量的加減+總價(jià)計(jì)算
- php 購(gòu)物車完整實(shí)現(xiàn)代碼
- js購(gòu)物車實(shí)現(xiàn)思路及代碼(個(gè)人感覺(jué)不錯(cuò))
- php 購(gòu)物車的例子
- jQuery實(shí)現(xiàn)加入購(gòu)物車飛入動(dòng)畫效果
- JQuery實(shí)現(xiàn)的購(gòu)物車功能(可以減少或者添加商品并自動(dòng)計(jì)算價(jià)格)
- 純jquery實(shí)現(xiàn)模仿淘寶購(gòu)物車結(jié)算
- 購(gòu)物車實(shí)現(xiàn)的幾種方式優(yōu)缺點(diǎn)對(duì)比
相關(guān)文章
ThinkPHP 連接Oracle數(shù)據(jù)庫(kù)的詳細(xì)教程[全]
最近收集了一些關(guān)于THinkPHP連接Oracle數(shù)據(jù)庫(kù)的問(wèn)題,有很多朋友按照連接mysql的方法來(lái)操作,導(dǎo)致有一些方法在Oreale中無(wú)法正常使用2012-07-07PHP去掉json字符串中的反斜杠\及去掉雙引號(hào)前的反斜杠
這篇文章主要介紹了PHP去掉json字符串中的反斜杠\及去掉雙引號(hào)前的反斜杠的相關(guān)資料,需要的朋友可以參考下2015-09-09PHP 使用pcntl和libevent 實(shí)現(xiàn)Timer功能
PHP 中實(shí)現(xiàn)Timer功能,中間使用到了php多線程,本文給出來(lái)pcntl的解釋。2013-10-10基于PHP的簡(jiǎn)單采集數(shù)據(jù)入庫(kù)程序【續(xù)篇】
在上篇 基于PHP的簡(jiǎn)單采集數(shù)據(jù)入庫(kù)程序 中提到采集新聞信息頁(yè)的列表數(shù)據(jù),接下來(lái)講講關(guān)于采集新聞具體內(nèi)容2014-07-07使用Laravel中的查詢構(gòu)造器實(shí)現(xiàn)增刪改查功能
這篇文章主要介紹了使用Laravel中的查詢構(gòu)造器實(shí)現(xiàn)增刪改查功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09thinkPHP通用控制器實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkPHP通用控制器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP針對(duì)數(shù)據(jù)庫(kù)的基本CURD操作方法的封裝實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11PHPWind9.0手動(dòng)屏蔽驗(yàn)證碼解決后臺(tái)關(guān)閉驗(yàn)證碼但是依然顯示的問(wèn)題
這篇文章主要介紹了PHPWind9.0手動(dòng)屏蔽驗(yàn)證碼解決后臺(tái)關(guān)閉驗(yàn)證碼但是依然顯示的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-08-08