php 购物车完整实现代码


Posted in PHP onJune 05, 2014

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]."1@";
  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 3water.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 3water.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="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></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="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'" size="25">
 </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="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></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="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></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="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"></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");
?>
PHP 相关文章推荐
PHP 设计模式系列之 specification规格模式
Jan 10 PHP
PHP实现微信网页授权开发教程
Jan 19 PHP
PHP5.2中PDO的简单使用方法
Mar 25 PHP
Yii2 ActiveRecord多表关联及多表关联搜索的实现
Jun 30 PHP
PHP中类属性与类静态变量的访问方法示例
Jul 13 PHP
Laravel最佳分割路由文件(routes.php)的方式
Aug 04 PHP
php自定义扩展名获取函数示例
Dec 12 PHP
PHP静态成员变量和非静态成员变量详解
Feb 14 PHP
php二维码生成以及下载实现
Sep 28 PHP
Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】
Feb 13 PHP
Discuz不使用插件实现简单的打赏功能
Mar 21 PHP
详解Laravel框架的依赖注入功能
May 27 PHP
PHP实例分享判断客户端是否使用代理服务器及其匿名级别
Jun 04 #PHP
浅谈PHP调用Webservice思路及源码分享
Jun 04 #PHP
利用谷歌 Translate API制作自己的翻译脚本
Jun 04 #PHP
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
Jun 04 #PHP
PHP 如何获取二维数组中某个key的集合
Jun 03 #PHP
PHP 二维数组根据某个字段排序的具体实现
Jun 03 #PHP
php 批量添加多行文本框textarea一行一个
Jun 03 #PHP
You might like
PHP开发负载均衡指南
2010/07/17 PHP
PHP常用设计模式之委托设计模式
2016/02/13 PHP
利用PHP如何统计Nginx日志的User Agent数据
2019/03/06 PHP
thinkphp5修改view到根目录实例方法
2019/07/02 PHP
简略的前端架构心得&amp;&amp;基于editor为例子的编码小技巧
2010/11/25 Javascript
js鼠标滑过弹出层的定位IE6bug解决办法
2012/12/26 Javascript
js格式化时间和js格式化时间戳示例
2014/02/10 Javascript
简单谈谈javascript中的变量、作用域和内存问题
2015/08/30 Javascript
JavaScript中对DOM节点的访问、创建、修改、删除
2015/11/16 Javascript
plupload+artdialog实现多平台上传文件
2016/07/19 Javascript
利用angularjs1.4制作的简易滑动门效果
2017/02/28 Javascript
NodeJs中express框架的send()方法简介
2017/06/20 NodeJs
深入探究node之Transform
2017/07/20 Javascript
Node使用Sequlize连接Mysql报错:Access denied for user ‘xxx’@‘localhost’
2018/01/03 Javascript
解决layui表格的表头不滚动的问题
2019/09/04 Javascript
vue控制多行文字展开收起的实现示例
2019/10/11 Javascript
javascript实现前端分页效果
2020/06/24 Javascript
vue等两个接口都返回结果再执行下一步的实例
2020/09/08 Javascript
[48:24]完美世界DOTA2联赛PWL S3 Forest vs INK ICE 第一场 12.09
2020/12/12 DOTA
python爬虫爬取某站上海租房图片
2018/02/04 Python
在IPython中执行Python程序文件的示例
2018/11/01 Python
Django中使用 Closure Table 储存无限分级数据
2019/06/06 Python
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
2020/02/11 Python
python用opencv 图像傅里叶变换
2021/01/04 Python
CSS3区域模块region相关编写示例
2015/08/28 HTML / CSS
理肤泉美国官网:La Roche-Posay
2018/01/17 全球购物
护士自我鉴定
2013/10/23 职场文书
网络教育自我鉴定
2013/11/01 职场文书
工作的心得体会
2013/12/31 职场文书
心得体会范文
2014/01/04 职场文书
理工类毕业自我鉴定
2014/02/20 职场文书
大学迎新晚会主持词
2014/03/24 职场文书
保护环境的标语
2014/06/09 职场文书
2015年清明节网上祭英烈活动总结
2015/03/26 职场文书
高考1977观后感
2015/06/04 职场文书
2016学习全国教书育人楷模先进事迹心得体会
2016/01/21 职场文书