php 购物车完整实现代码

1、商品展示页面

代码如下:

<table width="255" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="130" rowspan="6"><div align="center">

<?php

if(trim($info[tupian]==""))

{

echo "暂无图片";

}

else

{

?>

<img src="<?php echo $info[tupian];?>" width="130" height="100" border="0">

<?php

}

?>

</div></td>

<td width="20" height="16"> </td>

<td width="113"><font color="EF9C3E">【<?php echo $info[mingcheng];?>】</font></td>

</tr>

<tr>

<td height="16"> </td>

<td><font color="910800">【市场价:<?php echo $info[shichangjia];?>】</font></td>

</tr>

<tr>

<td height="16"> </td>

<td><font color="DD4679">【会员价:<?php echo $info[huiyuanjia];?>】</font></td>

</tr>

<tr>

<td height="16"> </td>

<td>【<a href="lookinfo.php?id=<?php echo $info[id];?>">查看信息</a>】</td>

</tr>

<tr>

<td height="16"> </td>

<td>【<a href="addgouwuche.php?id=<?php echo $info[id];?>">放入购物车</a>】</td>

</tr>

<tr>

<td height="16"> </td>

<td><font color="13589B">【剩余数量:

<?php

if(($info[shuliang]-$info[cishu])>0)

{

echo ($info[shuliang]-$info[cishu]);

}

else

{

echo "已售完";

}

?>】</font></td>

</tr>

</table>

<?php

}

?>

</table>

2、文件addgouwuche.php

复制代码代码如下:

<?php

session_start();

include("conn.php");

if($_SESSION[username]=="")

{

echo "<script>alert(‘请先登录后购物!‘);history.back();</script>";

exit;

}

$id=strval($_GET[id]);

$sql=mysql_query("select * from shangpin where id=‘".$id."‘",$conn);

$info=mysql_fetch_array($sql);

if($info[shuliang]<=0)

{

echo "<script>alert(‘该商品已经售完!‘);history.back();</script>";

exit;

}

$array=explode("@",$_SESSION[producelist]);

for($i=0;$i<count($array)-1;$i++)

{

if($array[$i]==$id)

{

echo "<script>alert(‘该商品已经在您的购物车中!‘);history.back();</script>";

exit;

}

}

$_SESSION[producelist]=$_SESSION[producelist].$id."@";

$_SESSION[quatity]=$_SESSION[quatity]."[email protected]";

header("location:gouwu1.php");

?>

3、文件gouwu1.php

代码如下:

<?php

session_start();

if($_SESSION[username]=="")

{

echo "<script>alert(‘请先登录,后购物!‘);history.back();</script>";

exit;

}

?>

<?php

include("top.php");

?>

<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td width="200" height="438" valign="top" bgcolor="#E8E8E8"><div align="center">

<?php include("left.php");?>

</div></td>

<td width="10" background="images/line2.gif"> </td>

<td width="590" valign="top"><table width="550" height="10" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td> </td>

</tr>

</table>

<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">

<form name="form1" method="post" action="gouwu1.php">

<tr>

<td height="25" bgcolor="#555555"><div align="center" style="color: #FFFFFF"><?php echo $_SESSION[username];?>的购物车</div></td>

</tr>

<tr>

<td bgcolor="#555555"><table width="500" border="0" align="center" cellpadding="0" cellspacing="1">

<?php

session_start();

session_register("total");

if($_GET[qk]=="yes")

{

$_SESSION[producelist]="";

$_SESSION[quatity]="";

}

$arraygwc=explode("@",$_SESSION[producelist]);

$s=0;

for($i=0;$i<count($arraygwc);$i++)

{

$s+=intval($arraygwc[$i]);

}

if($s==0 )

{

echo "<tr>";

echo" <td height=‘25‘ colspan=‘6‘ bgcolor=‘#FFFFFF‘ align=‘center‘>您的购物车为空!</td>";

echo"</tr>";

}

else

{

?>

<tr>

<td width="125" height="25" bgcolor="#FFFFFF"><div align="center">商品名称</div></td>

<td width="52" bgcolor="#FFFFFF"><div align="center">数量</div></td>

<td width="64" bgcolor="#FFFFFF"><div align="center">市场价</div></td>

<td width="64" bgcolor="#FFFFFF"><div align="center">会员价</div></td>

<td width="51" bgcolor="#FFFFFF"><div align="center">折扣</div></td>

<td width="66" bgcolor="#FFFFFF"><div align="center">小计</div></td>

<td width="71" bgcolor="#FFFFFF"><div align="center">操作</div></td>

</tr>

<?php

/**

* 购物车 商品数量管理

* Edit www.jbxue.com

*/

$total=0;

$array=explode("@",$_SESSION[producelist]);

$arrayquatity=explode("@",$_SESSION[quatity]);

while(list($name,$value)=each($_POST))

{

for($i=0;$i<count($array)-1;$i++)

{

if(($array[$i])==$name)

{

$arrayquatity[$i]=$value;

}

}

}

$_SESSION[quatity]=implode("@",$arrayquatity);

for($i=0;$i<count($array)-1;$i++)

{

$id=$array[$i];

$num=$arrayquatity[$i];

if($id!="")

{

$sql=mysql_query("select * from shangpin where id=‘".$id."‘",$conn);

$info=mysql_fetch_array($sql);

$total1=$num*$info[huiyuanjia];

$total+=$total1;

$_SESSION["total"]=$total;

?>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info[mingcheng];?></div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center">

<input type="text" name="<?php echo $info[id];?>" size="2" class="inputcss" value=<?php echo $num;?>>

</div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info[shichangjia];?>元</div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info[huiyuanjia];?>元</div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo @(ceil(($info[huiyuanjia]/$info[shichangjia])*100))."%";?></div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info[huiyuanjia]*$num."元";?></div></td>

<td height="25" bgcolor="#FFFFFF"><div align="center"><a href="removegwc.php?id=<?php echo $info[id]?>">移除</a></div></td>

</tr>

<?php

}

}

?>

<tr>

<td height="25" colspan="8" bgcolor="#FFFFFF"><div align="right">

<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td width="125"><div align="center">

<input type="submit" value="更改商品数量" class="buttoncss">

</div></td>

<td width="125"><div align="center"><a href="gouwu2.php">去收银台</a></div></td>

<td width="125"><div align="center"><a href="gouwu1.php?qk=yes">清空购物车</a></div></td>

<td width="125"><div align="left">总计:<?php echo $total;?></div></td>

</tr>

</table>

</div></td>

</tr>

<?php

}

?>

</table></td>

</tr>

</form>

</table></td>

</tr>

</table>

3、文件gouwu2.php

、代码如下:

<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td width="200" height="438" valign="top" bgcolor="#E8E8E8"><div align="center">

<?php include("left.php");?>

</div></td>

<td width="10" background="images/line2.gif"> </td>

<td width="590" valign="top"><table width="550" height="15" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td> </td>

</tr>

</table>

<table width="550" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td height="25" bgcolor="#555555"><div align="center" style="color: #FFFFFF">收货人信息</div></td>

</tr>

<tr>

<td height="300" bgcolor="#555555"><table width="550" height="300" border="0" align="center" cellpadding="0" cellspacing="1">

<script language="javascript">

/**

* 购物车 收货人信息

* Edit www.jbxue.com

*/

function chkinput(form)

{

if(form.name.value=="")

{

alert("请输入收货人姓名!");

form.name.select();

return(false);

}

if(form.dz.value=="")

{

alert("请输入收货人地址!");

form.dz.select();

return(false);

}

if(form.yb.value=="")

{

alert("请输入收货人邮编!");

form.yb.select();

return(false);

}

if(form.tel.value=="")

{

alert("请输入收货人联系电话!");

form.tel.select();

return(false);

}

if(form.email.value=="")

{

alert("请输入收货人E-mail地址!");

form.email.select();

return(false);

}

if(form.email.value.indexOf("@")<0)

{

alert("收货人E-mail地址格式输入错误!");

form.email.select();

return(false);

}

return(true);

}

</script>

<form name="form1" method="post" action="savedd.php" onSubmit="return chkinput(this)">

<tr>

<td width="100" height="25" bgcolor="#FFFFFF"><div align="center">收货人姓名:</div></td>

<td width="183" bgcolor="#FFFFFF"><div align="left"><input type="text" name="name" size="25" class="inputcss" style="</div></td>

<td width="86" bgcolor="#FFFFFF"><div align="center">性别:</div></td>

<td width="176" bgcolor="#FFFFFF"><div align="left">

<select name="***">

<option selected value="男">男</option>

<option value="女">女</option>

</select>

</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">详细地址:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input name="dz" type="text" class="inputcss" id="dz" style="

</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">邮政编码:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="yb" size="25" class="inputcss" style="</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">联系电话:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="tel" size="25" class="inputcss" style="</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">电子邮箱:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left"><input type="text" name="email" size="25" class="inputcss" style="</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">送货方式:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left">

<select name="shff" id="shff">

<option selected value="普通平邮">普通平邮</option>

<option value="特快专递">特快专递</option>

<option value="送货上门">送货上门</option>

<option value="个人送货">个人送货</option>

<option value="E-mail">E-mail</option>

</select>

</div></td>

</tr>

<tr>

<td height="25" bgcolor="#FFFFFF"><div align="center">支付方式:</div></td>

<td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left">

<select name="zfff" id="zfff">

<option selected value="建设银行汇款">建设银行汇款</option>

<option value="交通银行汇款">交通银行汇款</option>

<option value="邮局汇款">邮局汇款</option>

<option value="网上支付">网上支付</option>

</select>

</div></td>

</tr>

<tr>

<td height="100" bgcolor="#FFFFFF"><div align="center">简单留言:</div></td>

<td height="100" colspan="3" bgcolor="#FFFFFF"><div align="left">

<textarea name="ly" cols="60" rows="8" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=‘#ffffff‘" onMouseOut="this.style.backgroundColor=‘#e8f4ff‘"></textarea>

</div></td>

</tr>

<tr>

<td height="25" colspan="4" bgcolor="#FFFFFF"><div align="center"><input type="submit" value="提交订单" class="buttoncss">

</div></td>

</tr>

</form>

</table></td>

</tr>

</table></td>

</tr>

</table>

<?php

if($_GET[dingdanhao]!="")

{ $dd=$_GET[dingdanhao];

session_start();

$array=explode("@",$_SESSION[producelist]);

$sum=count($array)*20+260;

echo" <script language=‘javascript‘>";

echo" window.open(‘showdd.php?dd=‘+‘".$dd."‘,‘newframe‘,‘top=150,left=200,width=600,height=".$sum.",menubar=no,toolbar=no,location=no,scrollbars=no,status=no ‘)";

echo "</script>";

}

?>

4、数据库配置文件conn.php

代码如下:

<?php

$conn=mysql_connect("localhost","root","") or die("数据库服务器连接错误".mysql_error());

mysql_select_db("shop",$conn) or die("数据库访问错误".mysql_error());

mysql_query("set character set gb2312");

mysql_query("set names gb2312");

?>

时间: 2024-08-04 12:44:02

php 购物车完整实现代码的相关文章

Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)

转载请注明出处:http://blog.csdn.net/anxpp/article/details/51512200,谢谢! 本文会从传统的BIO到NIO再到AIO自浅至深介绍,并附上完整的代码讲解. 下面代码中会使用这样一个例子:客户端发送一段算式的字符串到服务器,服务器计算后返回结果到客户端. 代码的所有说明,都直接作为注释,嵌入到代码中,看代码时就能更容易理解,代码中会用到一个计算结果的工具类,见文章代码部分. 相关的基础知识文章推荐: Linux 网络 I/O 模型简介(图文) Jav

00微信公众平台 - 以上五大功能整合,完整的代码。

一.代码实现如下(主程序代码,其他include代码见各个功能函数中) <?php /** * wechat php test * version 1 */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); //$wechatObj->valid(); class wechatCallbackapiTest

asp完整登陆代码

asp完整登陆代码<%option explicit强制浏览器重新访问服务器下载页面,而不是从缓存读取页面Response.Buffer = True Response.Expires = -1Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache" 主要是使随机出现的图片数字随机%><!--#include file="inc/c

创建二叉查找树的完整C代码

BST 基本概念 二叉查找树(Binary Search Tree),又称二叉排序树(Binary Sort Tree),亦称二查搜索书. 它或者是一棵空树:或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值: (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值: (3)左.右子树也分别为二叉排序树: 简单的说就是:左孩子<双亲结点<右孩子. 因此,对查找二叉树进行中序遍历,得到的是一个从小到大排序的数列. 创建BST的完整C代码 /* 创

通过代码管理工具 git 完成一次完整的代码管理过程

1.从公共远程fork一份自己的本地远程之后,从本地远程 clone 到本地 2.将本地代码跟公共远程代码做关联配置 git remote add upstream https://github.com/volojs/create-template.git 3.此时查看本地代码所配置的远程有哪些 git remote -v origin https://github.com/suqiuhui/create-template (fetch) origin https://github.com/su

BST二叉排序树的查找和删除的完整C代码

二叉排序树的查找算法 假定二叉排序树的根节点指针为root,给定的关键字值为key,则查找算法可描述为: 置初值:p = root : 如果 key = p -> data ,则查找成功,算法结束: 否则,如果key < p->data ,而且 p 的左子树非空,则将 p 的左子树根送 p ,转步骤 2 :否则,查找失败,算法结束: 否则,如果 key > p->data ,而且 p 的右子树非空,则将 p 的右子树根送 p ,转步骤 2 :否则,查找失败,算法结束. //B

mser 最大稳定极值区域(文字区域定位)算法 附完整C代码

mser 的全称:Maximally Stable Extremal Regions 第一次听说这个算法时,是来自当时部门的一个同事, 提及到他的项目用它来做文字区域的定位,对这个算法做了一些优化. 也就是中文车牌识别开源项目EasyPR的作者liuruoze,刘兄. 自那时起就有一块石头没放下,想要找个时间好好理理这个算法. 学习一些它的一些思路. 因为一般我学习算法的思路:3个做法, 第一步,编写demo示例. 第二步,进行算法移植或效果改进. 第三步,进行算法性能优化. 然后在这三个过程中

音频降噪算法 附完整C代码

降噪是音频图像算法中的必不可少的. 目的肯定是让图片或语音 更加自然平滑,简而言之,美化. 图像算法和音频算法 都有其共通点. 图像是偏向 空间 处理,例如图片中的某个区域. 图像很多时候是以二维数据为主,矩形数据分布. 音频更偏向 时间 处理,例如语音中的某短时长. 音频一般是一维数据为主,单声道波长. 处理方式也是差不多,要不单通道处理,然后合并,或者直接多通道处理. 只是处理时候数据参考系维度不一而已. 一般而言, 图像偏向于多通道处理,音频偏向于单通道处理. 而从数字信号的角度来看,也可

音频自动增益 与 静音检测 算法 附完整C代码

前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用于评估一定长度音频的音量强度, 而分析之后,很多类似的需求,肯定是做音频增益,提高音量诸如此类做法. 不过在项目实测的时候,其实真的很难定标准, 到底在什么样的环境下,要增大音量,还是降低. 在通讯行业一般的做法就是采用静音检测, 一旦检测为静音或者噪音,则不做处理,反之通过一定的策略进行处理. 这里就涉及到两个算法,一个是静音检测,一个是音频增益. 增益其实没什么好说的,类似于数据归一化拉伸的做法. 静音检