php实现购物车shopping的完整示例代码
分类:PHP_Python| 发布:佚名| 查看:213 | 发表时间:2014/10/30
废话不多少,直接上购物车的代码如下,测试可完整运行:
1、商品展示页面
代码如下:
01 | < table width = "255" border = "0" cellspacing = "0" cellpadding = "0" > |
03 | < td width = "130" rowspan = "6" >< div align = "center" > |
05 | if(trim($info[tupian]=="")) |
12 | < img src="<?php echo $info[tupian];?>" width="130" height="100" border="0"> |
17 | < td width = "20" height = "16" > </ td > |
18 | < td width = "113" >< font color = "EF9C3E" >【<? php echo $info[mingcheng];?>】</ font ></ td > |
21 | < td height = "16" > </ td > |
22 | < td >< font color = "910800" >【市场价:<? php echo $info[shichangjia];?>】</ font ></ td > |
25 | < td height = "16" > </ td > |
26 | < td >< font color = "DD4679" >【会员价:<? php echo $info[huiyuanjia];?>】</ font ></ td > |
29 | < td height = "16" > </ td > |
30 | < td >【< a href="lookinfo.php?id=<?php echo $info[id];?>">查看信息</ a >】</ td > |
33 | < td height = "16" > </ td > |
34 | < td >【< a href="addgouwuche.php?id=<?php echo $info[id];?>">放入购物车</ a >】</ td > |
37 | < td height = "16" > </ td > |
38 | < td >< font color = "13589B" >【剩余数量: |
40 | if(($info[shuliang]-$info[cishu])>0) |
42 | echo ($info[shuliang]-$info[cishu]); |
2、文件addgouwuche.php
代码如下:
05 | if ( $_SESSION [username]== "" ) |
07 | echo "<script>alert('请先登录后购物 - camnpr!');history.back();</script>" ; |
10 | $id = strval ( $_GET [id]); |
11 | $sql =mysql_query( "select * from shangpin where id='" . $id . "'" , $conn ); |
12 | $info =mysql_fetch_array( $sql ); |
15 | echo "<script>alert('该商品已经售完!');history.back();</script>" ; |
18 | $array = explode ( "@" , $_SESSION [producelist]); |
19 | for ( $i =0; $i < count ( $array )-1; $i ++) |
23 | echo "<script>alert('该商品已经在您的购物车中!');history.back();</script>" ; |
27 | $_SESSION [producelist]= $_SESSION [producelist]. $id . "@" ; |
28 | $_SESSION [quatity]= $_SESSION [quatity]. "1@" ; |
30 | header( "location:gouwu1.php" ); |
3、文件gouwu1.php
代码如下:
003 | if ( $_SESSION [username]== "" ) |
005 | echo "<script>alert('请先登录,后购物 - 郑州网建 - camnpr!');history.back();</script>" ; |
012 | <table width= "800" height= "438" border= "0" align= "center" cellpadding= "0" cellspacing= "0" > |
014 | <td width= "200" height= "438" valign= "top" bgcolor= "#E8E8E8" ><div align= "center" > |
015 | <?php include ( "left.php" );?> |
017 | <td width= "10" background= "images/line2.gif" > </td> |
018 | <td width= "590" valign= "top" ><table width= "550" height= "10" border= "0" align= "center" cellpadding= "0" cellspacing= "0" > |
023 | <table width= "500" border= "0" align= "center" cellpadding= "0" cellspacing= "0" > |
024 | <form name= "form1" method= "post" action= "gouwu1.php" > |
026 | <td height= "25" bgcolor= "#555555" ><div align= "center" style= "color: #FFFFFF" ><?php echo $_SESSION [username];?>的购物车</div></td> |
029 | <td bgcolor= "#555555" ><table width= "500" border= "0" align= "center" cellpadding= "0" cellspacing= "1" > |
032 | session_register( "total" ); |
035 | $_SESSION [producelist]= "" ; |
036 | $_SESSION [quatity]= "" ; |
038 | $arraygwc = explode ( "@" , $_SESSION [producelist]); |
040 | for ( $i =0; $i < count ( $arraygwc ); $i ++) |
042 | $s += intval ( $arraygwc [ $i ]); |
047 | echo " <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>" ; |
054 | <td width= "125" height= "25" bgcolor= "#FFFFFF" ><div align= "center" >商品名称</div></td> |
055 | <td width= "52" bgcolor= "#FFFFFF" ><div align= "center" >数量</div></td> |
056 | <td width= "64" bgcolor= "#FFFFFF" ><div align= "center" >市场价</div></td> |
057 | <td width= "64" bgcolor= "#FFFFFF" ><div align= "center" >会员价</div></td> |
058 | <td width= "51" bgcolor= "#FFFFFF" ><div align= "center" >折扣</div></td> |
059 | <td width= "66" bgcolor= "#FFFFFF" ><div align= "center" >小计</div></td> |
060 | <td width= "71" bgcolor= "#FFFFFF" ><div align= "center" >操作</div></td> |
067 | $array = explode ( "@" , $_SESSION [producelist]); |
068 | $arrayquatity = explode ( "@" , $_SESSION [quatity]); |
070 | while (list( $name , $value )=each( $_POST )) |
072 | for ( $i =0; $i < count ( $array )-1; $i ++) |
074 | if (( $array [ $i ])== $name ) |
076 | $arrayquatity [ $i ]= $value ; |
080 | $_SESSION [quatity]=implode( "@" , $arrayquatity ); |
081 | for ( $i =0; $i < count ( $array )-1; $i ++) |
085 | $num = $arrayquatity [ $i ]; |
089 | $sql =mysql_query( "select * from shangpin where id='" . $id . "'" , $conn ); |
090 | $info =mysql_fetch_array( $sql ); |
091 | $total1 = $num * $info [huiyuanjia]; |
093 | $_SESSION [ "total" ]= $total ; |
096 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><?php echo $info [mingcheng];?></div></td> |
097 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" > |
098 | <input type= "text" name= "<?php echo $info[id];?>" size= "2" class = "inputcss" value=<?php echo $num ;?>> |
100 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><?php echo $info [shichangjia];?>元</div></td> |
101 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><?php echo $info [huiyuanjia];?>元</div></td> |
102 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><?php echo @( ceil (( $info [huiyuanjia]/ $info [shichangjia])*100)). "%" ;?></div></td> |
103 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><?php echo $info [huiyuanjia]* $num . "元" ;?></div></td> |
104 | <td height= "25" bgcolor= "#FFFFFF" ><div align= "center" ><a href= "removegwc.php?id=<?php echo $info[id]?>" >移除</a></div></td> |
111 | <td height= "25" colspan= "8" bgcolor= "#FFFFFF" ><div align= "right" > |
112 | <table width= "500" height= "25" border= "0" align= "center" cellpadding= "0" cellspacing= "0" > |
114 | <td width= "125" ><div align= "center" > |
115 | <input type= "submit" value= "更改商品数量" class = "buttoncss" > |
117 | <td width= "125" ><div align= "center" ><a href= "gouwu2.php" >去收银台</a></div></td> |
118 | <td width= "125" ><div align= "center" ><a href= "gouwu1.php?qk=yes" >清空购物车</a></div></td> |
119 | <td width= "125" ><div align= "left" >总计:<?php echo $total ;?></div></td> |
3、文件gouwu2.php
代码如下:
001 | < table width = "800" height = "438" border = "0" align = "center" cellpadding = "0" cellspacing = "0" > |
003 | < td width = "200" height = "438" valign = "top" bgcolor = "#E8E8E8" >< div align = "center" > |
004 | <? php include("left.php");?> |
006 | < td width = "10" background = "images/line2.gif" > </ td > |
007 | < td width = "590" valign = "top" >< table width = "550" height = "15" border = "0" align = "center" cellpadding = "0" cellspacing = "0" > |
012 | < table width = "550" border = "0" align = "center" cellpadding = "0" cellspacing = "0" > |
014 | < td height = "25" bgcolor = "#555555" >< div align = "center" style = "color: #FFFFFF" >收货人信息</ div ></ td > |
017 | < td height = "300" bgcolor = "#555555" >< table width = "550" height = "300" border = "0" align = "center" cellpadding = "0" cellspacing = "1" > |
018 | < script language = "javascript" > |
022 | function chkinput(form) |
024 | if(form.name.value=="") |
031 | if(form.dz.value=="") |
038 | if(form.yb.value=="") |
045 | if(form.tel.value=="") |
047 | alert("请输入收货人联系电话!"); |
052 | if(form.email.value=="") |
054 | alert("请输入收货人E-mail地址!"); |
059 | if(form.email.value.indexOf("@")< 0 ) |
061 | alert("收货人E-mail地址格式输入错误!"); |
069 | < form name = "form1" method = "post" action = "savedd.php" onSubmit = "return chkinput(this)" > |
071 | < td width = "100" height = "25" bgcolor = "#FFFFFF" >< div align = "center" >收货人姓名:</ div ></ td > |
072 | < td width = "183" bgcolor = "#FFFFFF" >< div align = "left" >< input type = "text" name = "name" size = "25" class = "inputcss" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" ></ div ></ td > |
073 | < td width = "86" bgcolor = "#FFFFFF" >< div align = "center" >性别:</ div ></ td > |
074 | < td width = "176" bgcolor = "#FFFFFF" >< div align = "left" > |
076 | < option selected value = "男" >男</ option > |
077 | < option value = "女" >女</ option > |
082 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >详细地址:</ div ></ td > |
083 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" >< input name = "dz" type = "text" class = "inputcss" id = "dz" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" size = "25" > |
087 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >邮政编码:</ div ></ td > |
088 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" >< input type = "text" name = "yb" size = "25" class = "inputcss" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" ></ div ></ td > |
091 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >联系电话:</ div ></ td > |
092 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" >< input type = "text" name = "tel" size = "25" class = "inputcss" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" ></ div ></ td > |
095 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >电子邮箱:</ div ></ td > |
096 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" >< input type = "text" name = "email" size = "25" class = "inputcss" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" ></ div ></ td > |
099 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >送货方式:</ div ></ td > |
100 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" > |
101 | < select name = "shff" id = "shff" > |
102 | < option selected value = "普通平邮" >普通平邮</ option > |
103 | < option value = "特快专递" >特快专递</ option > |
104 | < option value = "送货上门" >送货上门</ option > |
105 | < option value = "个人送货" >个人送货</ option > |
106 | < option value = "E-mail" >E-mail</ option > |
111 | < td height = "25" bgcolor = "#FFFFFF" >< div align = "center" >支付方式:</ div ></ td > |
112 | < td height = "25" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" > |
113 | < select name = "zfff" id = "zfff" > |
114 | < option selected value = "建设银行汇款" >建设银行汇款</ option > |
115 | < option value = "交通银行汇款" >交通银行汇款</ option > |
116 | < option value = "邮局汇款" >邮局汇款</ option > |
117 | < option value = "网上支付" >网上支付</ option > |
122 | < td height = "100" bgcolor = "#FFFFFF" >< div align = "center" >简单留言:</ div ></ td > |
123 | < td height = "100" colspan = "3" bgcolor = "#FFFFFF" >< div align = "left" > |
124 | < textarea name = "ly" cols = "60" rows = "8" class = "inputcss" style = "background-color:#e8f4ff " onMouseOver = "this.style.backgroundColor='#ffffff'" onMouseOut = "this.style.backgroundColor='#e8f4ff'" ></ textarea > |
128 | < td height = "25" colspan = "4" bgcolor = "#FFFFFF" >< div align = "center" >< input type = "submit" value = "提交订单" class = "buttoncss" > |
138 | if($_GET[dingdanhao]!="") |
139 | { $dd=$_GET[dingdanhao]; |
142 | $ array = explode ("@",$_SESSION[producelist]); |
143 | $ sum = count ($array)*20+260; |
144 | echo" <script language = 'javascript' >"; |
145 | 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 ')"; |
4、数据库配置文件conn.php
代码如下:
2 | $conn =mysql_connect( "localhost" , "root" , "" ) or die ( "数据库服务器连接错误" .mysql_error()); |
3 | mysql_select_db( "shop" , $conn ) or die ( "数据库访问错误" .mysql_error()); |
4 | mysql_query( "set character set gb2312" ); |
5 | mysql_query( "set names gb2312" ); |