📄 cart.php
字号:
<?
//本文件是购物车类的定义
class Cart
{
var $Cartitem; //此变量用来存放商品数据,实质是二位数组
var $count; //商品数量
var $owner; //用户ID
var $time; //时间
var $cartid; //购物车编号
//初始化结构函数
function Cart()
{
$this->count=0;
if(isset($userid))
{
$this->owner=$userid;
}
else
{
$this->owner=0;
}
$this->time=date("Y-m-d");
$this->cartid=time();
}
//添加商品的函数
function add_item($ProductID)
{
//设定标志变量
$flag=FALSE;
for($i=1;$i<=$this->count;$i++)
{
//检查在购物车里是否已有相同的商品
//如果有的话,设定标志为真
//相应的商品数量加一
if($this->Cartitem[$i]["productid"]==$ProductID)
{
$this->Cartitem[$i]["quantity"]++;
$flag=TRUE;
break;
}
}
//如果标志变量为假,说明在购物车里没有相同的商品
//想购物车里面加入商品名、id、价格和数量。
while(!$flag)
{
$connect=mysql_connect("127.0.0.1","","");
mysql_select_db("ebusiness");
$query="selects ProductPrice,ProductName from Product where ProductID='".$ProductID."'";
$result=mysql_query($query);
$res=mysql_fetch_object($result);
$price=$res->ProductPrice;
$pname=$res->ProductName;
mysql_close($connect);
$this->count++;
$this->Cartitem[$count]["productid"]=$ProductID;
$this->Cartitem[$count]["quantity"]=1;
$this->Cartitem[$count]["price"]=$price;
$this->Cartitem[$count]["productname"]=$pname;
}
}
//删除购物车中某一商品的函数
function remove_item($ProductID)
{
//设置标志变量
$flag=FALSE;
for($i=1;$i<=$this->count;$i++)
{
//寻找删除商品的ID
if($this->Cartitem[$i]["productid"]==$ProductID)
$j=$i;
$flag=true;
break;
}
if($flag)
(
//执行删除操作,实质是把删除项之后的每一项向前移。
for($i=$j;$i<=($this->count-1);$i++)
{
$this->Cartitem[$i]=$this->Cartitemp[$i+1];
}
$this->count--;
}
}
//函数设定用户ID,其作用是用于用户在购物后才登录的情况
function set_owner()
{
$this->owner==$userid;
}
//显示购物车的函数
function showcart()
{
echo'<form method="post" action="showall.php">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr align="center">
<td width="100" height="19">商品名</td>
<td width="100" height="19">数量</td>
<td width="100" height="19">价格</td>
<td width="100" height="19">总计</td>
<td width="100" height="19">删除</td>
</tr>
<tr align="center">';
$total=0;
for($i=1;$i<=$this->count;$i++)
{
echo'<td width="100">'.$this->Cartitem[$i]["productname"].';</td>';
echo'<td width="100">
<input type="text" name="a'.$i.'" size="7" maxlength="7"
value="'.$this->Cartitem[$i]["quantity"].'">
</td>';
echo'<td width="100">'.$this->Cartitem[$i]["price"].'</td>';
$sum=$this->Cartitem[$i]["quantity"]*$this->Cartitem[$i]["price"];
echo'<td width="100">'.$sum.'</td>';
echo'<td width="100"><a href=update.php?job=del&id='.$this->Cartitem[$i]["productid"]>.'</td>';
echo '</tr>';
}
echo'</table>';
echo'<p>总金额为:'.$total.'元。</p>';
echo'<p><input type="hidden" name="job" value="update">
<input type="image" border="0" name="submit" src="/pic/update.gif" width="32" height="20">
<a href='.$PHP_SELF.'><img src="/pic/goon.gif" width="32" height="20"><a>
<a href=check.php?total='.$total.'><img src="/pic/check.gif" width="32" height="20"><a>
<a href=showall.php?job=clear><img src="/pic/cleat.gif" width="32" height="20"><a>
</p>';
echo'</form>';
}
//更新购物车的函数
function updatecart($cartinfo)
{
//这个变量用来统计要删除的商品的数目
$delcount=0;
for($i=1;$i<=$this->count;$i++)
{
//改变商品的数量
$this->Cartitem[$i]["quantity"]=$cartinfo[$i];
//如果数量为零的话,记录下来
if($cartinfo[$i]==0)
{
$delcount++;
$del[$delcount]=$this->Cartitem[$i]["productid"];
}
}
//删除数量为零的商品
for($j=1;$j<=$delcount;$i++)
{
$this->remove_item($del[$j]);
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -