1. php session实现购物车的原理
session_start;
然后原理就是,建立一个数组,$_SESSION['wupin'][]=选中的物品的ID,然后就可以了,如果用户取消,就把他选择取消的那个ID的键从数组里删除就行。
2. 购物车代码怎么写php
写就行了,跟普通的一样,不就是数据库的增删改查吗
3. php 购物车代码~呢
<?php
class Shopcar
{
//商品列表
public $proctList=array();
/**
*
* @param unknown_type $proct 传进来的商品
* @return true 购物车里面没有该商品
*/
public function checkProct($proct)
{
for($i=0;$i<count($this->proctList);$i++ )
{
if($this->proctList[$i]['name']==$proct['name'])
return $i;
}
return -1;
}
//添加到购物车
public function add($proct)
{
$i=$this->checkProct($proct);
if($i==-1)
array_push($this->proctList,$proct);
else
$this->proctList[$i]['num']+=$proct['num'];
}
//删除
public function delete($proct)
{
$i=$this->checkProct($proct);
if($i!=-1)
array_splice($this->proctList,$i,1);
}
//返回所有的商品的信息
public function show()
{
return $this->proctList;
}
}
你可以去后盾人平台看看,里面的东西不错
4. 【高分】急求用php写的购物车代码!!!!!(十万火急)如果您提供的好用还有加分!!!
我也要弄一个这种购物车,
我去写个,贴出来,【嘿嘿,今天上午新写的】。
我懒得新建数据库,用的是我的数据库。
你按照我的改一下就能用了
本人水平有限,高手请指正。
你,大,爷的,虽然不咋地,保证能用
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
经过调试,
//$my->add_cart(45,3,"茶几系列");//新增购物
//$my->updata_cart(13,13,8); //更新购物
//$my->del_cart(12,5,'Guest'); //删除一种购物
//$my->empty_cart('Guest'); //清空购物车
$ok=$my->get_cart('Guest'); //返回购物车
这些都可用
-------------------------------------------------------------------
<?php
class Cart
{
public $totalCost=0; //商品总金额
function cart($host,$usr,$pwd,$db)
{
mysql_connect($host,$usr,$pwd) or die(mysql_error);
mysql_select_db($db) or die(mysql_error);
mysql_query("SET Names GBk");
//只要有人访问,就自动清除一天前所有没付款的订单;
$sql="delete FROM shopcart WHERE TO_DAYS( NOW( )) - TO_DAYS( ptime ) >=1 and payment=0";
mysql_query($sql);
}
// 弹出提示
function alter($Str,$Url)
{
echo "<Script language='JavaScript'> alert('".$Str."');</Script>";
echo "<meta http-equiv=refresh content=0;URL=".$Url.">";
}
//增加购物;三个参数:pid:产品ID,ptl:产品数量,pcid:产品类别
//查询数据库,是否存在此人在本日内订过本产品
//如果订过,那么数量累加,否则插入一个数据库行
function add_cart($pid,$ptl=1,$pcid)
{
if($ptl>=100 || $ptl<=0)
{
$this->alter("最多买99件,最少1件","index.php");
die();
}
if(!$_SESSION['usr']) { $usr='Guest';}
else { $usr=$_SESSION['usr'];}
$sql="select * from shopcart where pid='".$pid."' and usr='".$usr."' and pcid='".$pcid."'";
$ex=mysql_query($sql);
$ex1=mysql_fetch_array($ex);
if(!$ex1)
{
$sql="select * from proct where ID='".$pid."' and class1='".$pcid."'";
$ok=mysql_query($sql);
$rs=mysql_fetch_array($ok);
if($rs)
{
$totalCost= $rs['Price'] * $ptl;
$sql="insert into shopcart(usr,pid,pname,ptl,price,pcid,psum,payment) Values(";
$sql.="'".$usr."',";
$sql.="'".$rs['ID']."',";
$sql.="'".$rs['Name']."',";
$sql.="'".$ptl."',";
$sql.="'".$rs['Price']."',";
$sql.="'".$rs['Class1']."',";
$sql.="'".$totalCost."',";
$sql.="'0')";
mysql_query($sql) or die(mysql_error());
if($ok) { $this->alter("购物成功","index.php"); }
else { $this->alter("购物失败","index.php"); }
}
else
{
$this->alter("不存在的商品,或者参数错误","index.php");
die();
}
}
else
{
$sql="update shopcart set ptl= ptl+1,psum = psum+price where ID='".$ex1['ID']."'";
mysql_query($sql);
$this->alter("更新数量成功","index.php");
}
}
//更新购物车的单个产品的数量;
function updata_cart($cid,$ptl,$pid)
{
if($ptl>=100||$ptl<=0)
{
$this->alter('产品数量不对!','index.php');
die();
}
$sql="select * from shopcart where ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!ok) { alter("参数发生错误","index.php");}
else
{
$sql="update shopcart set ptl='".$ptl."',psum=price * '".$ptl."' where ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!ok) { $this->alter("更新失败","index.php");}
else { $this->alter("更新成功","index.php");}
}
}
function del_cart($cid,$pid,$usr)
{
$sql="delete from shopcart where usr='".$usr."' and ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!$ok) {$this->alter("删除失败","index.php");}
else {$this->alter("删除成功","index.php");}
}
function empty_cart($usr)
{
$sql="delete from shopcart where usr='".$usr."'";
mysql_query($sql) or die(mysql_error);
}
function get_cart($usr)
{
$sql="select * from shopcart where usr='".$usr."'";
$ok=mysql_query($sql);
return $ok;
}
}
$my = new Cart("localhost","root","root","mybbs");
//$my->add_cart(45,3,"茶几系列");
//$my->updata_cart(13,13,8);
//$my->del_cart(12,5,'Guest');
//$my->empty_cart('Guest');
$ok=$my->get_cart('Admin');
echo "usr pid pname ptl price pcid psum payment ptime <br><hr><br>";
while($rs=mysql_fetch_array($ok))
{
echo $rs[1]."->".$rs[2]."->".$rs[3]."->".$rs[4]."->".$rs[5]."->".$rs[6]."->".$rs[7]."->".$rs[8]."->".$rs[9]."<br>";
}
?>
、、、、、、、、、、、、、、、、、SQL、、、、、、、、、、、、、、
CREATE TABLE IF NOT EXISTS `shopcart` (
`ID` int(10) NOT NULL auto_increment,
`usr` varchar(50) NOT NULL,
`pid` int(5) NOT NULL,
`pname` varchar(100) NOT NULL,
`ptl` int(3) NOT NULL,
`price` decimal(50,2) NOT NULL default '0.00',
`pcid` varchar(100) NOT NULL,
`psum` decimal(50,2) NOT NULL default '0.00',
`payment` tinyint(1) NOT NULL,
`ptime` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
)
proct 里面用的ID CLASS1是
`ID` int(6) NOT NULL auto_increment,
`Class1` varchar(20) NOT NULL,
`Price` int(6) NOT NULL,
5. PHP 高手 请进来看下这段购物车代码
$sql="SELECT name,price FROM proct WHERE id='$id'";
$id是变量,php中虽然双引号和单引号都能表示字符串,但是不同的内是,单引号不能解析变量,也就容是说'$aaaa'表示的就是字符串$aaaa,而不会解析字符串!
改为:
$sql="SELECT name,price FROM proct WHERE id=$id";
6. 求PHP里的TP5的购物车代码
可以参考如下代码
<?php
classCartextendsThink{
//当前购物车名
public$sessionName;
//购物车总价格
public$totalPrice
publicfunction__construct($sessionName)
{
$this->sessionName=$sessionName;
if(!isset($_SESSION[$this->sessionName]))
{
$_SESSION[$this->sessionName]="";
}
}
//获取购物车的信息
publicfunctiongetCart(){
$cur_cart_array=$_SESSION[$this->sessionName];
return$cur_cart_array;
}
//获取购物车商品清单
publicfunctiongetCartList()
{
$cur_cart_array=$_SESSION[$this->sessionName];
if($cur_cart_array!="")
{
$mode_goods_data=M("goods_data");
$len=count($cur_cart_array);
for($i=0;$i<$len;$i++)
{
$goodsid=$cur_cart_array[$i]["id"];
$num=$cur_cart_array[$i]["num"];
$query="select(selectsfilenamefromgoods_picwheregoodsid=a.goodsidorderbysnodesclimit0,1)assfilename,b.clsnameasclsname,a.goodsidasgoodsid,a.goodsnameasgoodsname,a.PriceasPrice,a._dataaleftjoingoods_clsbona.Clsid=b.clsidwherea.goodsid=$goodsid";
$list=$mode_goods_data->query($query);
$list[0]["qty"]=$num;
$list[0]["amount"]=$num*$list[0]["Price"];
$cartList[$i]=$list[0];
$totalPrice+=$list[0]["amount"];
}
//返回商品总价格
$this->totalPrice=$totalPrice;
return$cartList;
}
}
//加入购物车,购物车的商品id和购物车的商品数量
publicfunctionaddcart($goods_id,$goods_num){
$cur_cart_array=$_SESSION[$this->sessionName];
if($cur_cart_array=="")
{
$cart_info[0]["id"]=$goods_id;//商品id保存到二维数组中
$cart_info[0]["num"]=$goods_num;//商品数量保存到二维数组中
$_SESSION[$this->sessionName]=$cart_info;
}
else
{
//返回数组键名倒序取最大
$ar_keys=array_keys($cur_cart_array);
$len=count($ar_keys);
$max_array_keyid=$ar_keys[$len-1]+1;
//遍历当前的购物车数组
//遍历每个商品信息数组的0值,如果键值为0且货号相同则购物车该商品已经添加
$is_exist=$this->isexist($goods_id,$goods_num,$cur_cart_array);
if($is_exist==false)
{
$cur_cart_array[$max_array_keyid]["id"]=$goods_id;
$cur_cart_array[$max_array_keyid]["num"]=$goods_num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
else
{
$arr_exist=explode("/",$is_exist);
$id=$arr_exist[0];
$num=$arr_exist[1];
$cur_cart_array[$id]["num"]=$num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
}
}
//判断购物车是否存在相同商品
publicfunctionisexist($id,$num,$array)
{
$isexist=false;
foreach($arrayas$key1=>$value)
{
foreach($valueas$key=>$arrayid)
{
if($key=="id"&&$arrayid==$id)
{
$num=$value["num"]+$num;
$isexist=$key1."/".$num;
}
}
}
return$isexist;
}
thinkphp开发使得我们比较容易的去进行了
//从购物车删除
publicfunctiondelcart($goods_array_id){
//回复序列化的数组
$cur_goods_array=$_SESSION[$this->sessionName];
//删除该商品在数组中的位置
unset($cur_goods_array[$goods_array_id]);
$_SESSION[$this->sessionName]=$cur_cart_array;
//使数组序列化完整的保存到cookie中
}
//清空购物车
publicfunctionemptycart(){
$_SESSION[$this->sessionName]="";
}
//修改购物车货品数量
publicfunctionupdate_cart($up_id,$up_num){
//回复序列化的数组
$cur_goods_array=$_SESSION[$this->sessionName];
$cur_goods_array[$up_id]["num"]=$up_num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
}
?>
7. 购物车源码思路PHP
首先确定购物车中商品结构(都有哪些字段)。
比如:商品ID,商品名称,数量,单价回等等。
然后,将这些答结构定义在数组里
array(
商品ID1=>array('name'=>'商品','num'=>1,'price'=>100),
商品ID2=>array('name'=>'商品','num'=>1,'price'=>100),
)
然后把这个大的数组保存在SESSION里。就可以了。
PHP有很多数组操作函数用起来也很方便。
8. php关于用数据库作为购物车的原理
我来解答一下你复的疑惑 买了两个产品制。那就是执行了两次 insert into temp_table (uid,proctid,pnum,poneprice,ptotalprice)如果 proctid相同则, pnum = pnum+1; ptotalprice = pnum*poneprice 假设前提是 当前两条记录的产品不同,那么购物车列表则是循环读取temp_table列出现有符合条件之产品,数量,价格。 修改2个产品数量的时候, 提交后,同样的文本框pnum为一个数组,proctid为一个数组 获取pnum,proctid,并且用 split分析后, 分别update update temp_table set pnum='".$pnum[0]."',ptotalprice='..省略.' where uid=自己的uid and proctid='".$proctid[0]."'注意,这里数组下标要对应好,你可以用个循环。 最后,当订单下好之后,要把临时表的数据转移到正式表中,并且清理掉当前这个用户临时表的内容即可。
9. php 购物车代码一般是用什么方法实现
将你的session,看成抄一个数组。
$_SESSION['user_id']=array{
['商品袭id']=array{
'价格'=>501,
},
['商品id']=array{
'价格'=>502,
},
['商品id']=array{
'价格'=>503,
}
}
以上保存了3件商品的信息,如果用户id为20的人,期间修改了商品的信息,就根据user_id与商品id,去修改你的这个数组,最后生成订单的时候,insert入库。
10. 求一个php session 购物车类代码
最好还是建一个专门的购物车表,不要用session存储,这样虽然方便,但是会员在其他地方登录或者换个时间购物车内的东西就不存在了,这样太不好啊。