新聞中心
PHP 高手 請進(jìn)來看下這段購物車代碼
$sql="SELECT name,price FROM product WHERE id='$id'";
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)涼州,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
$id是變量,php中雖然雙引號和單引號都能表示字符串,但是不同的是,單引號不能解析變量,也就是說'$aaaa'表示的就是字符串$aaaa,而不會(huì)解析字符串!
改為:
$sql="SELECT name,price FROM product WHERE id=$id";
【高分】急求用php寫的購物車代碼!?。。。。ㄊf火急)如果您提供的好用還有加分!??!
我也要弄一個(gè)這種購物車,
我去寫個(gè),貼出來,【嘿嘿,今天上午新寫的】。
我懶得新建數(shù)據(jù)庫,用的是我的數(shù)據(jù)庫。
你按照我的改一下就能用了
本人水平有限,高手請指正。
你,大,爺?shù)?,雖然不咋地,保證能用
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
經(jīng)過調(diào)試,
//$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");
//只要有人訪問,就自動(dòng)清除一天前所有沒付款的訂單;
$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."";
}
//增加購物;三個(gè)參數(shù):pid:產(chǎn)品ID,ptl:產(chǎn)品數(shù)量,pcid:產(chǎn)品類別
//查詢數(shù)據(jù)庫,是否存在此人在本日內(nèi)訂過本產(chǎn)品
//如果訂過,那么數(shù)量累加,否則插入一個(gè)數(shù)據(jù)庫行
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 product 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("不存在的商品,或者參數(shù)錯(cuò)誤","index.php");
die();
}
}
else
{
$sql="update shopcart set ptl= ptl+1,psum = psum+price where ID='".$ex1['ID']."'";
mysql_query($sql);
$this-alter("更新數(shù)量成功","index.php");
}
}
//更新購物車的單個(gè)產(chǎn)品的數(shù)量;
function updata_cart($cid,$ptl,$pid)
{
if($ptl=100||$ptl=0)
{
$this-alter('產(chǎn)品數(shù)量不對!','index.php');
die();
}
$sql="select * from shopcart where ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!ok) { alter("參數(shù)發(fā)生錯(cuò)誤","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 brhrbr";
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`)
)
product 里面用的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這個(gè)代碼怎么寫,腦殼都想破了。
$count_price = 多少錢;//購物車總金額
if($count_price =100 $count_price200)
{
$count_price = $count_price-2;
}else if($count_price=200 $count_price300){
$count_price = $count_price -5;
}else if($count_price=300){
$count_price = $count_price - 7;
}
求PHP里的TP5的購物車代碼
可以參考如下代碼
?php
class?Cart?extends?Think?{
//當(dāng)前購物車名
public?$sessionName;
//購物車總價(jià)格
public?$totalPrice
public?function?__construct($sessionName)
{
$this-sessionName=$sessionName;
if(!isset($_SESSION[$this-sessionName]))
{
$_SESSION[$this-sessionName]="";
}
}
//獲取購物車的信息
public?function?getCart(){
$cur_cart_array=$_SESSION[$this-sessionName];
return?$cur_cart_array;
}
//獲取購物車商品清單
public?function?getCartList()
{
$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?(select?sfilename?from?goods_pic?where?goodsid=a.goodsid?order?by?sno?desc?limit?0,1)?as?sfilename,b.clsname?as?clsname,a.goodsid?as?goodsid,a.goodsname?as?goodsname,a.Price?as?Price,a.Storageqty?as?Storageqty?from?goods_data?a?left?join?goods_cls?b?on?a.Clsid=b.clsid?where?a.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"];
}
//返回商品總價(jià)格
$this-totalPrice=$totalPrice;
return?$cartList;
}
}
//加入購物車,購物車的商品id和購物車的商品數(shù)量
public?function?addcart($goods_id,$goods_num){
$cur_cart_array=$_SESSION[$this-sessionName];
if($cur_cart_array=="")
{
$cart_info[0]["id"]=$goods_id;//商品id保存到二維數(shù)組中
$cart_info[0]["num"]=$goods_num;//商品數(shù)量保存到二維數(shù)組中
$_SESSION[$this-sessionName]=$cart_info;
}
else
{???
//返回?cái)?shù)組鍵名倒序取最大
$ar_keys=array_keys($cur_cart_array);
$len=count($ar_keys);
$max_array_keyid=$ar_keys[$len-1]+1;
//遍歷當(dāng)前的購物車數(shù)組
//遍歷每個(gè)商品信息數(shù)組的0值,如果鍵值為0且貨號相同則購物車該商品已經(jīng)添加
$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;
}?
}
}
//判斷購物車是否存在相同商品
public?function?isexist($id,$num,$array)
{
$isexist=false;
foreach($array?as?$key1=$value)
{
foreach($value?as?$key=$arrayid)
{
if($key=="id"??$arrayid==$id)
{
$num=$value["num"]+$num;
$isexist=$key1."/".$num;
}
}
}
return?$isexist;
}
thinkphp開發(fā)使得我們比較容易的去進(jìn)行了
//從購物車刪除
public?function?delcart($goods_array_id){
//回復(fù)序列化的數(shù)組
$cur_goods_array=$_SESSION[$this-sessionName];
//刪除該商品在數(shù)組中的位置
unset($cur_goods_array[$goods_array_id]);
$_SESSION[$this-sessionName]=$cur_cart_array;
//使數(shù)組序列化完整的保存到cookie中
}
//清空購物車
public?function?emptycart(){
$_SESSION[$this-sessionName]="";
}
//修改購物車貨品數(shù)量???
public?function?update_cart($up_id,$up_num){
//回復(fù)序列化的數(shù)組
$cur_goods_array=$_SESSION[$this-sessionName];??
$cur_goods_array[$up_id]["num"]=$up_num;
$_SESSION[$this-sessionName]=$cur_cart_array;
}
}
?
當(dāng)前標(biāo)題:數(shù)據(jù)庫php購物車代碼 數(shù)據(jù)庫php購物車代碼是什么
當(dāng)前地址:http://www.dlmjj.cn/article/dohoido.html