php 购物车完整实现代码

 更新时间:2014年6月7日 11:51  点击:1906

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 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="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");
?>

[!--infotagslink--]

相关文章

  • ASP.NET购物车实现过程详解

    这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • php语言实现redis的客户端

    php语言实现redis的客户端与服务端有一些区别了因为前面介绍过服务端了这里我们来介绍客户端吧,希望文章对各位有帮助。 为了更好的了解redis协议,我们用php来实现...2016-11-25
  • jQuery+jRange实现滑动选取数值范围特效

    有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS实现的简洁纵向滑动菜单(滑动门)效果

    本文实例讲述了JS实现的简洁纵向滑动菜单(滑动门)效果。分享给大家供大家参考,具体如下:这是一款纵向布局的CSS+JavaScript滑动门代码,相当简洁的手法来实现,如果对颜色不满意,你可以试着自己修改CSS代码,这个滑动门将每一...2015-10-21
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • jQuery+slidereveal实现的面板滑动侧边展出效果

    我们借助一款jQuery插件:slidereveal.js,可以使用它控制面板左右侧滑出与隐藏等效果,项目地址:https://github.com/nnattawat/slideReveal。如何使用首先在页面中加载jquery库文件和slidereveal.js插件。复制代码 代码如...2015-03-15
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP+jQuery翻板抽奖功能实现

    翻板抽奖的实现流程:前端页面提供6个方块,用数字1-6依次表示6个不同的方块,当抽奖者点击6个方块中的某一块时,方块翻转到背面,显示抽奖中奖信息。看似简单的一个操作过程,却包含着WEB技术的很多知识面,所以本文的读者应该熟...2015-10-21
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • SQLMAP结合Meterpreter实现注入渗透返回shell

    sqlmap 是一个自动SQL 射入工具。它是可胜任执行一个广泛的数据库管理系统后端指印, 检索遥远的DBMS 数据库等,下面我们来看一个学习例子。 自己搭建一个PHP+MYSQ...2016-11-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24