php之購物車類思路及代碼(php立即購買和購物車功能)

本文目錄一覽:

【高分】急求用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 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(“不存在的商品,或者參數錯誤”,”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 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,

php購物車和列印訂單代碼怎麼寫

不就是幾個表單頁么,有啥難寫的。。這裡不寫代碼了,寫寫思路吧。購物車,這種東西就是說客戶買的東西要放在一個表單裡面,這裡我們需要一個客戶提交一個表單,買了什麼東西,多少個,價錢如何,訂單日期多少。。。把這些參數傳遞到訂單顯示頁,好了,在這個頁將它們顯示出來。

求PHP里的TP5的購物車代碼

可以參考如下代碼

?php

class Cart extends Think {

//當前購物車名

public $sessionName;

//購物車總價格

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”];

  }

  //返回商品總價格

  $this-totalPrice=$totalPrice;

    return $cartList;

 }

}

//加入購物車,購物車的商品id和購物車的商品數量

public function addcart($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;

         } 

       }

}

//判斷購物車是否存在相同商品

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開發使得我們比較容易的去進行了

//從購物車刪除

public function delcart($goods_array_id){

         //回復序列化的數組

     $cur_goods_array=$_SESSION[$this-sessionName];

        //刪除該商品在數組中的位置

     unset($cur_goods_array[$goods_array_id]);

         $_SESSION[$this-sessionName]=$cur_cart_array;

         //使數組序列化完整的保存到cookie中

}

//清空購物車

public function emptycart(){

$_SESSION[$this-sessionName]=””;

}

//修改購物車貨品數量   

public function update_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;

}

}

?

原創文章,作者:JSLTI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330507.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JSLTI的頭像JSLTI
上一篇 2025-01-16 15:46
下一篇 2025-01-16 15:46

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29

發表回復

登錄後才能評論