⑴ 【高分】急求用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,
⑵ 购物车满100减2元,200减5元 300减7元,php这个代码怎么写,脑壳都想破了。
$count_price = 多少钱;//购物车总金额
if($count_price >=100 && $count_price<200)
{
$count_price = $count_price-2;
}else if($count_price>=200 && $count_price<300){
$count_price = $count_price -5;
}else if($count_price>=300){
$count_price = $count_price - 7;
}
⑶ 如何用html css javascript php制作购物车
一两句说不清楚………
用html css做出商品样子和购物车样子,
然后用javascript来对买这个动作做处理,把商品的信息存起来,通过js把商品信息传递给购物车,让购物车能够显示;
另一方面把信息交给php,写入数据库。
我这么说你明白了么……
⑷ 跪求一份PHP购物车的源码 只要求不到 我就一直等 等到天荒等到地老
源码我这里没有。而且找出来的源码也未必适合你。
给你说一下购物车的思路吧,希望对你有帮助
1.现在互联网的购物车.一般都采用cookie来做的.
a.用户点击"加入购物车",把当前物品id,按照某种形式,写入cookie.
b.代码思路.$_COOKIE['car']可以理解为就是购物车.里面是物品id;
$car = $_COOKIE['car']; //先读取购物车
$car = $car ? explode(',',$car) : array(); //如果购物车有物品,则把物品转化成数组,如果没有则默认成空数组;
$car[] = $goods_id; //往购物车中增加该物品;
$car = implode(','.$car); //把购物车中的所有物品id,转化成字符串。这便于存贮在cookie中,直接存数组也可以。但是如果你需要把整个购物车信息加密的话,那就应该只有字符串了。
setcookie('car',$car,time()+3600); //购物车存1个小时,这个时间可以设置的长一点;
2.也可以通过数据库来实现;但前提是必须要用户登陆才可以。
table_car
uid
goods_id
datetime
1.每次用户点击“加入购物车”,往数据库中写入,该条信息就好了。
这种情况需要注意,在一定的时间段外,需要清空之前的信息。所以这里设置了datetime;
这个只是简单的购物车原理。里面还有一些其他细节。
1.比如说物品的数量。
2.cookie信息的加密和解密.
..........
3.才用数据库方式,需要清空哪些购物数据
⑸ thinkphp做的session购物车,求详细代码。有完整例子的可以发下。非常感谢
到thinkphp官网上找一个不就可以了,上面有很多的实例啊