導航:首頁 > 編程語言 > php購物車介面代碼

php購物車介面代碼

發布時間:2022-11-30 09:48:12

㈠ 【高分】急求用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,

㈡ 求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;
}
}
?>

㈢ 一個最簡單的用PHP實現購物車功能,請高手指點

這個感覺用cookie就好了啦
你一步步的跟蹤一下,看看是哪兒沒拿到值,可以在foreach之前把session的數組轉存到一個變數里

㈣ 請教關於PHP購物車代碼的資料庫表和欄位

PHP Code
<div id="procts-wrapper">
<h1>Procts</h1>
<div class="procts">
<?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$results = $mysqli->query("SELECT * FROM cart ORDER BY id ASC");
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="proct">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="proct-thumb"><img src="images/'.$obj->proct_img_name.'"></div>';
echo '<div class="proct-content"><h3>'.$obj->proct_name.'</h3>';
echo '<div class="proct-desc">'.$obj->proct_desc.'</div>';
echo '<div class="proct-info">';
echo 'Price '.$currency.$obj->price.' | ';
echo 'Qty <input type="text" name="proct_qty" value="1" size="3" />';
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></div>';
echo '<input type="hidden" name="proct_code" value="'.$obj->proct_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
</div>
<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["procts"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["procts"] as $cart_itm)
{
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php">Check-out!</a></span>';
echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
cart_update.php
PHP Code
<?php
session_start();
include_once("config.php");
//empty cart by distroying current session
if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1)
{
$return_url = base64_decode($_GET["return_url"]); //return url
session_destroy();
header('Location:'.$return_url);
}
//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$proct_code = filter_var($_POST["proct_code"], FILTER_SANITIZE_STRING); //proct code
$proct_qty = filter_var($_POST["proct_qty"], FILTER_SANITIZE_NUMBER_INT); //proct code
$return_url = base64_decode($_POST["return_url"]); //return url
//limit quantity for single proct
if($proct_qty > 10){
die('<div align="center">This demo does not allowed more than 10 quantity!<br /><a href="http://sanwebe.com/assets/paypal-shopping-cart-integration/">Back To Procts</a>.</div>');
}
//MySqli query - get details of item from db using proct code
$results = $mysqli->query("SELECT proct_name,price FROM cart WHERE proct_code='$proct_code' LIMIT 1");
$obj = $results->fetch_object();
if ($results) { //we have the proct info
//prepare array for the session variable
$new_proct = array(array('name'=>$obj->proct_name, 'code'=>$proct_code, 'qty'=>$proct_qty, 'price'=>$obj->price));
if(isset($_SESSION["procts"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["procts"] as $cart_itm) //loop through session array
{
if($cart_itm["code"] == $proct_code){ //the item exist in array
$proct[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$proct_qty, 'price'=>$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrive old info and prepare array for session var
$proct[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["procts"] = array_merge($proct, $new_proct);
}else{
//found user item in array list, and increased the quantity
$_SESSION["procts"] = $proct;
}
}else{
//create a new session var if does not exist
$_SESSION["procts"] = $new_proct;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["procts"]))
{
$proct_code = $_GET["removep"]; //get the proct code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["procts"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]!=$proct_code){ //item does,t exist in the list
$proct[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
//create a new proct list for cart
$_SESSION["procts"] = $proct;
}
//redirect back to original page
header('Location:'.$return_url);
}
?>
view_cart.php
PHP Code
<div class="view-cart">
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["procts"]))
{
$total = 0;
echo '<form method="post" action="paypal-express-checkout/process.php">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["procts"] as $cart_itm)
{
$proct_code = $cart_itm["code"];
$results = $mysqli->query("SELECT proct_name,proct_desc, price FROM cart WHERE proct_code='$proct_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="proct-info">';
echo '<h3>'.$obj->proct_name.' (Code :'.$proct_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->proct_desc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->proct_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$proct_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->proct_desc.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '</form>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
</div>

閱讀全文

與php購物車介面代碼相關的資料

熱點內容
攝影聖經pdf 瀏覽:565
稅3個點怎麼演算法 瀏覽:781
程序包msi可以反編譯嗎 瀏覽:813
linux設備使用 瀏覽:75
word圖紙加密 瀏覽:845
類似大明青年的程序員公眾號 瀏覽:41
ug編程之巴雷特第一講 瀏覽:980
安卓手機如何保護人臉識別 瀏覽:713
男友程序員每天都加班 瀏覽:620
全文檢索java 瀏覽:299
大數據用什麼編程語言 瀏覽:287
數據上傳伺服器有什麼方式 瀏覽:330
我的世界多人伺服器叫什麼 瀏覽:753
qt程序怎樣實現反編譯 瀏覽:734
手機安卓用戶是什麼意思 瀏覽:541
c單片機delay 瀏覽:192
文件夾壞了怎麼修補 瀏覽:166
熱血江湖無法連接伺服器怎麼辦 瀏覽:527
長河pdf 瀏覽:125
華為手機側欄怎麼減少app 瀏覽:486