php实现购物车shopping的完整示例代码

分类:PHP_Python| 发布:佚名| 查看:213 | 发表时间:2014/10/30

废话不多少,直接上购物车的代码如下,测试可完整运行:

1、商品展示页面

代码如下:
01<table width="255"  border="0" cellspacing="0" cellpadding="0">
02<tr>
03<td width="130" rowspan="6"><div align="center">
04<?php
05        if(trim($info[tupian]==""))
06     {
07       echo "暂无图片";
08     }
09     else
10     {
11?>
12<img src="<?php echo $info[tupian];?>" width="130" height="100" border="0">
13<?php
14  }
15?>
16</div></td>
17  <td width="20" height="16"> </td>
18  <td width="113"><font color="EF9C3E">【<?php echo $info[mingcheng];?>】</font></td>
19 </tr>
20 <tr>
21  <td height="16"> </td>
22  <td><font color="910800">【市场价:<?php echo $info[shichangjia];?>】</font></td>
23 </tr>
24 <tr>
25  <td height="16"> </td>
26  <td><font color="DD4679">【会员价:<?php echo $info[huiyuanjia];?>】</font></td>
27 </tr>
28 <tr>
29  <td height="16"> </td>
30  <td>【<a href="lookinfo.php?id=<?php echo $info[id];?>">查看信息</a>】</td>
31 </tr>
32 <tr>
33  <td height="16"> </td>
34  <td>【<a href="addgouwuche.php?id=<?php echo $info[id];?>">放入购物车</a>】</td>
35 </tr>
36 <tr>
37  <td height="16"> </td>
38  <td><font color="13589B">【剩余数量:
39      <?php 
40      if(($info[shuliang]-$info[cishu])>0)
41      {
42         echo ($info[shuliang]-$info[cishu]);
43      }
44      else
45      {
46         echo "已售完";
47      }
48      ?>】</font></td>
49 </tr>
50 </table>
51     <?php
52      }
53     ?>    
54</table>

2、文件addgouwuche.php

代码如下:
01<?php
02session_start();
03include("conn.php");
04 
05if($_SESSION[username]=="")
06 {
07  echo "<script>alert('请先登录后购物 - camnpr!');history.back();</script>"
08  exit;
09 }
10  $id=strval($_GET[id]);
11$sql=mysql_query("select * from shangpin where id='".$id."'",$conn); 
12$info=mysql_fetch_array($sql);
13if($info[shuliang]<=0)
14 {
15   echo "<script>alert('该商品已经售完!');history.back();</script>";
16   exit;
17 }
18  $array=explode("@",$_SESSION[producelist]);
19  for($i=0;$i<count($array)-1;$i++)
20    {
21  if($array[$i]==$id)
22   {
23      echo "<script>alert('该商品已经在您的购物车中!');history.back();</script>";
24   exit;
25   }
26 }
27  $_SESSION[producelist]=$_SESSION[producelist].$id."@";
28  $_SESSION[quatity]=$_SESSION[quatity]."1@";
29 
30  header("location:gouwu1.php");
31?>

3、文件gouwu1.php

代码如下:
001<?php
002 session_start();
003 if($_SESSION[username]=="")
004  {
005    echo "<script>alert('请先登录,后购物 - 郑州网建 - camnpr!');history.back();</script>";
006 exit;
007  }  
008?>
009<?php
010 include("top.php");
011?>
012<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0">
013  <tr>
014    <td width="200" height="438" valign="top" bgcolor="#E8E8E8"><div align="center">
015 <?php include("left.php");?>
016    </div></td>
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">
019      <tr>
020        <td> </td>
021      </tr>
022    </table>    
023      <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
024        <form name="form1" method="post" action="gouwu1.php">
025          <tr>
026 <td height="25" bgcolor="#555555"><div align="center" style="color: #FFFFFF"><?php echo $_SESSION[username];?>的购物车</div></td>
027          </tr>
028          <tr>
029 <td  bgcolor="#555555"><table width="500" border="0" align="center" cellpadding="0" cellspacing="1">
030<?php
031   session_start();
032     session_register("total");
033     if($_GET[qk]=="yes")
034     {
035        $_SESSION[producelist]="";
036     $_SESSION[quatity]=""
037     }
038      $arraygwc=explode("@",$_SESSION[producelist]);
039      $s=0;
040      for($i=0;$i<count($arraygwc);$i++)
041      {
042          $s+=intval($arraygwc[$i]);
043      }
044     if($s==0 )
045       {
046       echo "<tr>";
047   echo" <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>";
048   echo"</tr>";
049    }
050     else
051      
052   ?>
053<tr>
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>
061</tr>
062<?php
063/**
064 * 购物车 商品数量管理 @郑州网建
065*/
066$total=0;
067$array=explode("@",$_SESSION[producelist]);
068$arrayquatity=explode("@",$_SESSION[quatity]);
069 
070     while(list($name,$value)=each($_POST))
071        {
072       for($i=0;$i<count($array)-1;$i++)
073       {
074         if(($array[$i])==$name)
075      {
076        $arrayquatity[$i]=$value;  
077      }
078       }       
079     }
080    $_SESSION[quatity]=implode("@",$arrayquatity);
081    for($i=0;$i<count($array)-1;$i++)
082     
083 
084       $id=$array[$i];
085       $num=$arrayquatity[$i];
086 
087      if($id!="")
088       {
089       $sql=mysql_query("select * from shangpin where id='".$id."'",$conn);
090       $info=mysql_fetch_array($sql);
091       $total1=$num*$info[huiyuanjia];
092       $total+=$total1;
093       $_SESSION["total"]=$total;
094   ?>
095<tr>
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;?>>
099  </div></td>
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>
105</tr>
106<?php         
107         }
108     }
109?>
110<tr>
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">
113        <tr>
114          <td width="125"><div align="center">
115 <input type="submit" value="更改商品数量" class="buttoncss">
116          </div></td>
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>
120        </tr>
121      </table>
122  </div></td>
123 </tr>
124 <?php
125    }
126   ?>
127</table></td>
128</tr>
129</form>
130    </table></td>
131  </tr>
132</table>

3、文件gouwu2.php

代码如下:
001<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0">
002  <tr>
003    <td width="200" height="438" valign="top" bgcolor="#E8E8E8"><div align="center">
004 <?php include("left.php");?>
005    </div></td>
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">
008      <tr>
009        <td> </td>
010      </tr>
011    </table>
012      <table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
013        <tr>
014          <td height="25" bgcolor="#555555"><div align="center" style="color: #FFFFFF">收货人信息</div></td>
015        </tr>
016        <tr>
017          <td height="300" bgcolor="#555555"><table width="550" height="300" border="0" align="center" cellpadding="0" cellspacing="1">
018<script language="javascript">
019/**
020 * 购物车 收货人信息 @camnpr
021*/
022function chkinput(form)
023    {
024      if(form.name.value=="")
025       {
026      alert("请输入收货人姓名!");
027      form.name.select();
028      return(false);
029 
030    }
031    if(form.dz.value=="")
032       {
033      alert("请输入收货人地址!");
034      form.dz.select();
035      return(false);
036 
037    }
038    if(form.yb.value=="")
039       {
040      alert("请输入收货人邮编!");
041      form.yb.select();
042      return(false);
043 
044    }
045    if(form.tel.value=="")
046       {
047      alert("请输入收货人联系电话!");
048      form.tel.select();
049      return(false);
050 
051    }
052    if(form.email.value=="")
053       {
054      alert("请输入收货人E-mail地址!");
055      form.email.select();
056      return(false);
057 
058    }
059    if(form.email.value.indexOf("@")<0)
060     {
061        alert("收货人E-mail地址格式输入错误!");
062        form.email.select();
063        return(false);
064 
065     }
066    return(true);    
067    }     
068     </script>
069     <form name="form1" method="post" action="savedd.php" onSubmit="return chkinput(this)">
070      <tr>
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">
075 <select name="***">
076  <option selected value="男">男</option>
077  <option value="女">女</option>
078 </select>
079 </div></td>
080 </tr>
081 <tr>
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">
084 </div></td>
085 </tr>
086 <tr>
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>
089 </tr>
090 <tr>
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>
093 </tr>
094 <tr>
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>
097 </tr>
098 <tr>
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>
107      </select>
108</div></td>
109 </tr>
110 <tr>
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>
118 </select>
119 </div></td>
120 </tr>
121 <tr>
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>
125</div></td>
126 </tr>
127 <tr>
128 <td height="25" colspan="4" bgcolor="#FFFFFF"><div align="center"><input type="submit" value="提交订单" class="buttoncss">
129 </div></td>
130 </tr>
131   </form>
132          </table></td>
133        </tr>
134      </table></td>
135  </tr>
136</table>
137<?php
138 if($_GET[dingdanhao]!="")
139  {  $dd=$_GET[dingdanhao];
140     session_start();
141 
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 ')";
146 echo "</script>";
147 
148  }
149?>

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

代码如下:
1<?php
2$conn=mysql_connect("localhost","root","") or die("数据库服务器连接错误".mysql_error());
3mysql_select_db("shop",$conn) or die("数据库访问错误".mysql_error());
4mysql_query("set character set gb2312");
5mysql_query("set names gb2312");
6?>
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/1704.html

相关文章