php购物车功能
1. phpcms支持购物车功能吗
PHPCMS是开源程序
程序本身没神仿有购物车和类似功能
需要二次开发购物车功能或者直接购买世轿别人开发的PHPCMS购搜瞎肆物车插件安装到PHPCMS上
2. 求php增加购物车数量的加减,
伪代码:
前端:在你页面上数量那栏增加
<div class="quantity-form"><a href="javascript:void(0);" clstag="cart_num_down" class="decrement disabled" id="decrement_8888_526830_1_1">-</a>
<input autocomplete="off" type="text" class="itxt" value="1" id="changeQuantity_8888_526830_1_1_0" minnum="1">
<a href="javascript:void(0);" clstag="cart_num_up" class="increment" id="increment_8888_526830_1_1_0">+</a>
</div>
你的购物车是个循环列表,可以去得到,每个商品的信息:id是商品
function cart_num_up(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:-1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){
});
});
}
function cart_num_down(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){
});
});
}
点一次 加或减按钮,触发js向后端发起ajax请求:返回的是增加成功和失败状态和剩余数量;
CART_num.PHP
$id = (int)$_POST['id'];
$num = (int)$_POST['num'];
$uid =(int)$_POST['uid']; //有封装获取post或get函数更好
//查库看库存
$sort = get_kucun_num();
//查看购物车目前数量
$cart_num = get_cart_num(uid,id);
//判断库存
if($sort >$cart_num){
}else{
}
if($num<0){
//减法 $sql =“update cart set cart_num = cart_num-1 where uid =uid and id =id ”
if(($cart_num-1)>1){
}else{
}){
//加法 $sql =“update cart set cart_num = cart_num+1 where uid =uid and id =id ”
}else{
//不正确的请求
}
3. 求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;
}
}
?>
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中使用什么方法可以让购物车页面刷新时数量不增加
这要看你是怎么做的购物车?为何刷新时会增加数量呢?
一般我的做法是:
1.用ajax的方式实现.点击加入购物车.当然页面都不动.而是后台处理.
2.购物车的内容是根据已经保存的数据来显示的.和刷新根本没关系.
6. 设计一个购物车,包含html和php两个文件,html让客户输入个人信息以及显示商品信息。php将订单信息返回给
订单表,客户信息表,商品信息表,购物车表存放客户信息表,商品信息表的主键id,用来关联,并且建立一个数量的字段,用来计算总价。客户录入表单去提交页面添加判断然后跳转购物页,添加购物车的话直接基于数量和两个主键id去添加,下单的话基于时间戳生成订单编号和客户编号存放在订单表中,记得主外键关联
7. php 购物车。 在一产品列表上,任选多个产品,然后直接提交到购物车上。请问这个代码如何写,需要具体代码
可以用数组提交
表单中 这么写 <input name="a[]" type="checkbox" /> <input name="id[]" type="hidden" /><input name="num[]" />
关键是name=" "写成数组,具体写法看你的具体情况
后台
//循环处理 产品信息
foreach($a as $k => $v)
{
$now_id = $_POST['id'][$k];
$now_num = $_POST['num'][$k];
//下面你自己写
}
8. php 实现 购物车 提交到 订单的数据库
用户登陆后系统应该会分配一个session : id,这个id应该是改用于的用户表id。然后再做一个购物车的表,里面可以只考虑存商品id、用户id等关键信息;
当用户提交购买的时候再做相关的操作,具体可以参考淘宝、天猫的购物车已经后续付款效果。
9. PHP如何用Cookie做购物车
我有个session的例子你看看吧!!!你把session换成cookie就可以绝贺了!!!
//增加购物车物品
if($name==$_SESSION["car"][$name]["name"])
{
$_SESSION["car"][$name]["count"]++;
}
else{
$_SESSION["car"][$name]["name"]=$name;
$_SESSION["car"][$name]["maney"]=$maney;
$_SESSION["car"][$name]["count"]=1;
}
//销宏桥删除物品
if( $_SESSION["car"][$name]["count"]>1 && $dele=='1')
{
$_SESSION["car"][$a]["count"亏猛]--;
}
10. 大神们,出来吧。。PHP中购物车的功能实现,存放在数据库中是否会造成数据表数据过多,读取缓慢。
购物车新建一个表,外键到用户ID,这样可以有效控制冗余。