当前位置:首页 > 代码 > 正文

商城下方的加入购物车代码(购物车加减按钮代码)

admin 发布:2022-12-19 09:51 117


今天给各位分享商城下方的加入购物车代码的知识,其中也会对购物车加减按钮代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求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;

}

}

?

如何实现ecshop批量添加商品到购物车

第一步:

找到主机根目录下 /themes/default/library/goods_list.lbi 文件,在其中每个商品的前面增加一个复选框按钮。即默认模板大约第74行,增加代码:

input type=”checkbox” value=”{$goods.goods_id}” /

第二步:

增加可以添加选定商品到购物车的链接。位置任选,添加的代码如下:

a href=”javascript:addSelectedToCart(‘goodslist’)”Add selected products to cart

第三步:

添加循环将选定商品加入购物车的函数,打开/js/common.js文件,在第36行,加入如下代码:

/*

*添加选定商品到购物车

*/

function addSelectedToCart(p)

{

for ( var i=0; idocument.getElementById(p).elements.length; i++ )

{

var e = document.getElementById(p).elements[i];

if ( (e.type==’checkbox’)(!e.disabled) )

{

if ( e.checked )

{

addToCart(e.value) ;

}

}

}

}

按照以上三个步骤即可实现,其实Ecshop这款开源的商城系统在客户下单时添加商品到购物车使用的就是一个JS函数调用来提交数据的,在这里我们添加一个封装好的函数addToCart()来实现批量添加就可以实现啦。按照以上步骤修改好之后到网站后台更新下缓存就可以完美实现批量添加商品到购物车了。

jsp购物车代码

//shopping.html

html

headtitleshopping stor/title/head

body

form action="carts.jsp" target="post"

br

please select the item that you want to buy

br

select name="item"

optionbook:old man and the sea

optionx-box game machine

optionmp3 player

optioncce

optionbook:jsp programming

optioncd "the endless love"

optiondvd "gone with the wind"

/select

br

input type="submit" name="submit" value="add"

input type="submit" name="submit" value="remove"

/form

/body

/html

------------------------------------------------------------------

//carts.jsp

%@page contentType="text/html;charset=ISO8859_1" %

html

jsp:useBean id="cart" scope="session" class="test.DummyCart"/

jsp:setProperty name="cart" property="*"/

%

cart.processRequest();

%

br

ol

you have chosen these items:

%

String []items=cart.getItems();

for(int i=0;iitems.length;i++)

{

%

li%=items[i] %/li

%

}

%

/ol

hr

%@include file="shopping.htm" %

/html

---------------------------------------------------------------------//DummyCart.java

package test;

import javax.servlet.http.*;

import java.util.Vector;

import java.util.Enumeration;

public class DummyCart

{

Vector v = new Vector();

String submit=null;

String item= null;

private void addItem(String name)

{

v.addElement(name);

}

private void removeItem(String name)

{

v.removeElement(name);

}

public void setItem(String s)

{

item=s;

}

public void setSubmit(String s)

{

submit=s;

}

public String[] getItems()

{

String []s=new String[v.size()];

v.copyInto(s);

return s;

}

public void processRequest()

{

if(submit==null)

addItem(item);

if(submit.equals("add"))

addItem(item);

else if (submit.equals("remove"))

removeItem(item);

reset();

}

private void reset()

{

submit=null;

item=null;

}

}

----------------------------------------------------------------------

上面是一个简单的例子,功能都能实现,对网页效果要求更漂亮些的可做一些修改。

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

购物车的Java代码

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;public class ShoppingCartManager {

HashMapString, String hm=new HashMapString, String();

float totlePrice=0;

//添加book到购物车

public void addBook(String bookId,String bookQuantity){

if(hm.containsKey(bookId)){

int value=Integer.parseInt(hm.get(bookId));

value+=Integer.parseInt(bookQuantity);

hm.put(bookId, value+"");

}else{

hm.put(bookId, bookQuantity);

}

}

//修改数量

public void updateQuantity(String bookId,String bookQuantity){

hm.put(bookId, bookQuantity);

}

//获取购物车的所有信息 并计算总价

public ArrayListBookBean getShoppingCart(){

ArrayListBookBean al=new ArrayListBookBean();

IteratorString i=hm.keySet().iterator();

String ids="";

BookTableManager btm=new BookTableManager();

while(i.hasNext()){

ids=ids+","+i.next();

}

al= btm.selectByBookIds(ids);

totlePrice=0; //清空总价,防止无限累计

for(int j=0;jal.size();j++){

BookBean bb=al.get(j);

totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));

}

return al;

}

//获取总价

public float getTotlePrice(){

return totlePrice;

}

//根据ID获取数量

public String getQuantityById(String id){

String quantity=hm.get(id);

return quantity;

}

//清空购物车

public void clear(){

hm.clear();

}

//删除购物车中的一本书

public void deleteById(String id){

hm.remove(id);

}

}

商城下方的加入购物车代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于购物车加减按钮代码、商城下方的加入购物车代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/6241.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载