<?php
//购物车类
//支持自定义列名及列数
// 作者:天地小子 twt326@163.com
//转载或修改请保留原版权,谢谢
class twt_Cart
{
//类属性*************************************************************
var $sp_count;//商品类数
var $sp_sumgold;//商品总金额
var $fields;//自定义列
var $f_num;//自定义列数
var $f_key;//作为判断商品是否重复的关键字
var $f_goldkey;//商品金额字段名 用于商品统计信息计算时用
var $f_numkey;//商品数量字段名 用于商品统计信息计算时用
var $cart;
//构造函数
function twt_Cart()
{
//从SESSION中读出一些必须参数
session_start();
if (! session_is_registered("mycart")) session_register("mycart");
$mycart=$_SESSION["mycart"];
$this->sp_count=count($mycart);
if (! session_is_registered("mc_fields")) session_register("mc_fields");
$this->fields=$_SESSION["mc_fields"];
$this->f_num=count($this->fields)-1;
if (! session_is_registered("mc_key")) session_register("mc_key");
$this->f_key=$_SESSION["mc_key"];
if (! session_is_registered("mc_goldkey")) session_register("mc_goldkey");
$this->f_goldkey=$_SESSION["mc_goldkey"];
if (! session_is_registered("mc_numkey")) session_register("mc_numkey");
$this->f_numkey=$_SESSION["mc_numkey"];
$this->cart=$mycart;
$this->updatecart();
}
//类方法***************************************************************
//更新总金额
function updatecart()
{
session_start();
if (! session_is_registered("mycart")) return false;
$mycart=$_SESSION["mycart"];
$tmp=0;
for ($i=0;$i<$this->sp_count;$i++)
$tmp+=$mycart[$i][$this->f_goldkey]*$mycart[$i][$this->f_numkey];
$this->sp_sumgold=$tmp;
}
//设置自定义字段及初始化类 数组参数,以0开头
function setfield($tfield)
{
if (is_array($tfield))
{
$this->fields=$tfield;
//初始化一些值
$this->f_num=count($tfield)-1;
$this->f_key=$tfield[0];
$this->f_goldkey=$tfield[0];
$this->f_numkey=$tfield[0];
session_start();
$_SESSION["mc_fields"]=$tfield;
$_SESSION["mc_key"]=$tfield[0];
$_SESSION["mc_goldkey"]=$tfield[0];
$_SESSION["mc_numkey"]=$tfield[0];
}
else
return "参数传输错误,请传入从0开始下标的数组!";
}//setfield end
//设置关键字
function setkey($key,$numkey,$goldkey)
{
$this->f_key=$key;
$this->f_goldkey=$goldkey;
$this->f_numkey=$numkey;
session_start();
$_SESSION["mc_key"]=$key;
$_SESSION["mc_goldkey"]=$goldkey;
$_SESSION["mc_numkey"]=$numkey;
}
//清除购物篮
function clear()
{
$mycart=$_SESSION["mycart"];
unset ($mycart);
$_SESSION["mycart"]=$mycart;
session_unregister("mycart");
session_unregister("mc_fields");
session_unregister("mc_key");
session_unregister("mc_goldkey");
session_unregister("mc_numkey");
$this->cart=$mycart;
$this->sp_count=0;
$this->sp_sumgold=0;
}
//功能方法/////////////////////////////////////////////////////////
//添加商品 数组参数,以自定义的字段名这下标
function addone($splist)
{
if ((! is_array($splist)) || ((count($splist)-1) != $this->f_num))
return "参数传输错误,请传入以自定义字段名为下标且元数个数吻合的数组!";
session_start();
$mycart=$_SESSION["mycart"];
//判断商品是否已经存在,如果存在,则加上
$tflag=true;
for ($i=0;$i<$this->sp_count;$i++)
{
if ($mycart[$i][$this->f_key]==$splist[$this->f_key])
{
$tflag=false;
//增加商品个数
$mycart[$i][$this->f_numkey]+=$splist[$this->f_numkey];
$i=$this->sp_count;
}
}
//添加商品到列表中
if ($tflag)
{
for ($i=0;$i<=$this->f_num;$i++)
{
$mycart[$this->sp_count][$this->fields[$i]]=$splist[$this->fields[$i]];
}
$this->sp_count+=1;
}
$this->cart=$mycart;
$_SESSION["mycart"]=$mycart;
$this->updatecart();
}//add end
//删除一个商品
function delone($id)
{
session_start();
$mycart = $_SESSION["mycart"] ;
for ($i=$id;$i<$this->sp_count-1;$i++)
{
for ($j=0;$j<=$this->f_num;$j++)
$mycart[$i][$this->fields[$j]]=$mycart[$i+1][$this->fields[$j]];
}
unset( $mycart[$this->sp_count-1] ) ;
$this->sp_count-=1;
$_SESSION["mycart"] = $mycart ;
$this->cart=$mycart;
$this->updatecart();
}//del end
//修改一件商品的数量
function modifyone($id,$num)
{
session_start();
$mycart = $_SESSION["mycart"] ;
if (! empty($mycart[$id][$this->f_key]))
$mycart[$id][$this->f_numkey]=$num;
$this->cart=$mycart;
$_SESSION["mycart"]=$mycart;
$this->updatecart();
}
//修改一件商品的单价
function modifyprice($id,$num)
{
session_start();
$mycart = $_SESSION["mycart"] ;
if (! empty($mycart[$id][$this->f_key]))
$mycart[$id][$this->f_goldkey]=$num;
$this->cart=$mycart;
$_SESSION["mycart"]=$mycart;
$this->updatecart();
}//modifyprice end
//某件商品加1
function add1($id)
{
session_start();
$mycart = $_SESSION["mycart"] ;
$mycart[$id][$this->f_numkey]+=1;
$this->cart=$mycart;
$_SESSION["mycart"]=$mycart;
$this->updatecart();
}
//某件商品减1
function del1($id)
{
session_start();
$mycart = $_SESSION["mycart"] ;
$mycart[$id][$this->f_numkey]-=1;
$this->cart=$mycart;
$_SESSION["mycart"]=$mycart;
$this->updatecart();
}
}//class end
?>
下面是类的用法实例页面:
<?php
session_start();
include ("admin/config/twt_Cart.CLASS.php");
include ("admin/config/config.php");
include ("admin/config/function.php");
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的购物车</title>
</head>
<style>
table {
border: 0.5px solid;
}
.tbbb {
border: 0.5px solid #FFCCFF;
}
</style>
<script language="javascript">
<!--
//函数名:fucCheckNUM
//功能介绍:检查是否为大于0数字
//参数说明:要检查的数字
//返回值:1为是数字,0为不是数字
function fucCheckNUM(NUM)
{
var i,j,k,strTemp;
strTemp="0123456789.";
if ( NUM.length== 0)
return false;
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//说明有字符不是数字
return false;
}
}
//检查小数点的个数
k=0;
strTemp=".";
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j != -1)
{
//是小数点
k+=1;
}
if (k>0) return false;
}
if (NUM<=0) return false;
//说明是数字
return true;
}
function checknum(num)
{
if (! fucCheckNUM(num))
{
document.frmspin.f_num.value=1;
alert("请输入大于0的下整数!");
return false;
}
}
function modifyid(id)
{
document.frmsplist.action="mycart.php?action=modify&id="+id;
document.frmsplist.submit();
}
//-->
</script>
<body>
<?
$thiscart=new twt_Cart;
$field[0]="id";
$field[1]="name";
$field[2]="num";
$field[3]="gold";
$thiscart->setfield($field);
$thiscart->setkey("id","num","gold");
if ($_GET[action]=="incart")
{
$tlist["id"]=$_POST[f_id];
$tlist["name"]=$_POST[f_name];
$tlist["num"]=$_POST[f_num];
$tlist["gold"]=$_POST[f_gold];
$thiscart->addone($tlist);
}
if ($_GET[action]=="del")
{
$thiscart->delone($_GET[id]);
}
if ($_GET[action]=="modify")
{
$thiscart->modifyone($_GET[id],$_POST["t_num_$_GET[id]"]);
}
if ($_GET[action]=="clear")
{
$thiscart->clear();
}
if ($_GET[action]=="add1")
{
$thiscart->add1($_GET[id]);
}
if ($_GET[action]=="del1")
{
$thiscart->del1($_GET[id]);
}
if($_GET[action]=='indb')
{
if ($thiscart->sp_count==0)
{ $tmpstr="alert(\"没有定购任何商品!\");
window.close();";
echo getjavascript($tmpstr);
}
else
{
$sql[0]="delete from dglist where dgl_bjid=$_SESSION[wb_bjid]";
$sql[1]="delete from dinggou where dg_uid=$_SESSION[wb_id] and dg_jid=$_SESSION[wb_bjid]";
exesql($conf_db,$sql,$db,"清空旧的商品列表出错!","echo",true,"res");
//插入定购表
$sql="insert into dinggou set dg_uid=$_SESSION[wb_id],dg_jid=$_SESSION[wb_bjid]";
exesql($conf_db,$sql,$db,"提交定单出错!","echo",true,"res");
//插入商品
$sql=array();
for ($i=0;$i<$thiscart->sp_count;$i++)
{
$sql[$i]="insert into dglist set dgl_bjid=$_SESSION[wb_bjid],dgl_pid=".$thiscart->cart[$i]["id"].",dgl_num=".$thiscart->cart[$i]["num"];
// echo $sql[$i];
}
exesql($conf_db,$sql,$db,"提交商品订单出错!","popup",true,"res");
$thiscart->clear();
$tmpstr="alert(\"操作完成,购物车已经被清空!\");
window.open(\"http://".$_SESSION[wb_sip].":5026/dg|".rawurlencode($_SESSION[wb_bjno])."| \",\"订购商品\",\"width=500 height=300 revisable=no scrollbar=yes menubar=no status=no directions=no location=no toolbar=no\");
window.close();";
echo getjavascript($tmpstr);
}
}
?>
<div align="center"><img src="image/cart/behav_01.gif" width="20" height="14"><font color="#33CCFF">我的购物车-->商品列表</font><br/>
</div>
<table width="503" border="1" align="center" cellpadding="0" cellspacing="1" bordercolor="#CCCCFF">
<form name="frmsplist" method="post" action="mycart.php?action=indb">
<tr>
<td colspan="2"><div align="left"><a href="mycart.php?action=clear"><img src="image/cart/clear.gif" width="79" height="17" border="0"></a></div></td>
<td colspan="3"><div align="center"></div>
<div align="right">
<table width="91" border="1" cellpadding="0" cellspacing="3" bordercolor="#FFCCFF" bgcolor="#CCCCFF">
<tr>
<td width="81" bordercolor="#FFFF00"><div align="center">
<input type="image" name="submit" src="image/cart/done.gif" border="0">
</div></td>
</tr>
</table>
</div></td>
</tr>
<tr>
<td width="58"><div align="center">编号</div></td>
<td width="168"><div align="center">品名</div></td>
<td width="53"><div align="center">数量</div></td>
<td width="63"><div align="center">价格</div></td>
<td width="149"><div align="center">操作</div></td>
</tr>
<?php
for ($i=0;$i<$thiscart->sp_count;$i++)
{
?><tr>
<td><div align="center"><?php echo $i+1; ?></div></td>
<td><div align="center">
<input type="text" name="textfield" value="<?php echo $thiscart->cart[$i]["name"]; ?>" readonly>
</div></td>
<td><div align="center">
<input name="t_num_<?php echo $i; ?>" type="text" id="t_num_<?php echo $i; ?>" value="<?php echo $thiscart->cart[$i]["num"]; ?>" size=7>
</div></td>
<td><div align="right"><?php echo $thiscart->cart[$i]["gold"]; ?></div></td>
<td><div align="center"><a href="javascript:modifyid(<?php echo $i; ?>);">修改</a> <a href="mycart.php?action=add1&id=<?php echo $i; ?>">加1</a> <a href="mycart.php?action=del1&id=<?php echo $i; ?>">减1</a> <a href="mycart.php?action=del&id=<?php echo $i; ?>">删除</a> </div></td>
</tr><?php
}
?><tr>
<td colspan="2"><div align="center"> </div></td>
<td><div align="center">总额</div></td>
<td colspan="2"><div align="right"><?php echo $thiscart->sp_sumgold; ?>元
</div></td>
</tr>
</form>
</table>
<table width="503" border="0" align="center" cellpadding="6" cellspacing="0">
<tr>
<td><div align="right"><a href="#" target="_self"><img src="image/cart/goon.gif" width="79" height="17" border="0" usemap="#Map" onClick="javascript:window.close();"></a></div></td>
</tr>
</table>
<div align="center"><br/>
<?php
if ($_GET[action]=="dgsp")
{
?>
<img src="image/cart/behav_01.gif" width="20" height="14"><font color="#33CCFF">订购商品</font><br/>
</div>
<table width="425" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFCCFF">
<form name="frmspin" method="post" action="mycart.php?action=incart">
<tr>
<td width="71"><div align="right">
<input name="f_id" type="hidden" id="f_id" value="<?php echo $_GET[cpid]; ?>">
品名:</div></td><?php
//从数据库中读出产品信息
$sql="select * from shangpin where sp_id=$_GET[cpid]";
$rst=exesql($conf_db,$sql,$db,"未找到商品!","popup","yes","rst");
?>
<td width="205"><input name="f_name" type="text" id="f_name" readonly value="<?php echo $rst[sp_name]; ?>"></td>
<td width="141" rowspan="4" valign="middle"><div align="center"><img src="<?php
if (empty($rst[sp_img]))
echo $conf_fimgpath."defult.jpg";
else {
if ($rst[sp_imgflag]==1)
echo $conf_fimgpath.$_SESSION[wb_id]."/".$rst[sp_img];
else
echo $conf_fimgpath.$rst[sp_img];}
?>" width="120" height="120"></div></td>
</tr>
<tr>
<td><div align="right">价格:</div></td>
<td><input name="f_gold" type="text" id="f_gold" readonly value="<?php echo $rst[sp_gold]; ?>"></td>
</tr>
<tr>
<td><div align="right">介绍:</div></td>
<td><textarea name="textarea" cols="25" rows="4" wrap="VIRTUAL"><?php echo $rst[sp_text]; ?></textarea></td>
</tr>
<tr>
<td><div align="right">数量:</div></td>
<td><input name="f_num" type="text" id="f_num2" onChange="checknum(document.frmspin.f_num.value)" value="1" size="10"></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<!--<input type="submit" name="Submit2" value="放入购物车">-->
<input name="imageField" type="image" src="image/cart/buy.gif" width="79" height="17" border="0">
</div></td>
</tr>
</form>
</table>
<?php } ?>
</body>
</html>