php 购物车完整实现代码_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]."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.jb51.net
*/
$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.jb51.net
*/
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");
?>

时间: 2024-09-20 17:40:20

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

比较完整的微信开发php代码_php实例

本文实例为大家分享了微信开发php代码,供大家参考,具体内容如下 <?php //封装成一个微信接口类 class WeixinApi { private $appid; private $appsecret; //构造方法 初始化赋值 public function __construct($appid="",$appsecret="") { $this->appid = $appid; $this->appsecret = $appsecret

php购物车实现代码_php实例

ShopCar.php 复制代码 代码如下: <?php class Shopcar { //商品列表 public $productList=array(); /** * * @param unknown_type $product 传进来的商品 * @return true 购物车里面没有该商品 */ public function checkProduct($product) { for($i=0;$i<count($this->productList);$i++ ) { if($

CI框架入门示例之数据库取数据完整实现方法_php实例

本文实例讲述了CI框架入门示例之数据库取数据完整实现方法.是写给初学者看的,这是最简单可以调通的例子.分享给大家供大家参考.具体实现方法如下: 1.下载CI框架 2.配置 database.php配置: 为数据库服务器设置 connection 参数: 复制代码 代码如下: $db['default']['hostname'] = "your-db-host";  $db['default']['username'] = "your-username";  $db[

PHP编写daemon process详解及实例代码_php实例

今天下午在segmentfault.com看到一个提问,提问标题是"PHP怎么做服务化",其中问道php是不是只能以web方式调用.其实很多人对PHP的使用场景都有误解,认为php只能用于编写web脚本,实际上,从PHP4开始,php的使用场景早已不限于处理web请求. 从php的架构体系来说,php分为三个层次:sapi.php core和zend engine.php core本身和web没有任何耦合,php通过sapi与其它应用程序通信,例如mod_php就是为apache编写的

php 网上商城促销设计实例代码_php实例

大体的思想,每一个促销要新建一个促销类,有专门的开关来控制是否生效. 用商品里面的促销识别码来判断具体调用哪一个促销实例. 首先,在添加商品的时候,分两步,第一步是添加状态,第二步是把购物车内的商品显示这个状态. 一,添加步骤几个重要的点: 1,添加商品之前,遍历所有的促销互斥条件. 例如,某一款商品不可以和另一个商品同时在一个购物车内:或者某个用户权限,不可以购买特定的一件商品等等. 2,添加商品之前,选择特定的促销实例,来进行添加之前的操作. 注:第二点与第一点的区别在于1是要遍历所有的促销

php 微信公众平台开发模式实现多客服的实例代码_php实例

其实微信公众平台的多客服功能已经出来好久了,并且一出来的时候我就已经为自己的公众号实现了,原本以为大家都已经会了,但是今天还是有人问起这个多客服功能怎么使用,我找了下网上也没什么好的教程,今天我就给大家发一篇比较简单易懂的教程吧 在这篇微信公众平台开发教程中,我们将介绍如何使用开发模式实现多客服系统. 一.回复多客服消息 在新的微信协议中,开发模式也可以接入客服系统. 开发者如果需要让用户使用客服系统,需要在接收到用户发送的消息时,返回一个MsgType为transfer_customer_se

php 的反射详解及示例代码_php实例

最近在看java编程思想,看到类型信息这一章,讲到了类的信息以及反射的概念.顺便温故一下php的反射东西.手册是这样说的:"PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 此外,反射 API 提供了方法来取出函数.类和方法中的文档注释."当然手册上说的有些抽象!所谓的逆向说白就是能获取关于类.方法.属性.参数等的详细信息,包括注释! 文字总是那么枯燥,举个例子 class Foo { public $foo = 1; protected $b

PHP Ajax JavaScript Json获取天气信息实现代码_php实例

要在自己的网站上添加一个天气预报功能,是一个很普通的需求,实现起来也不是很难.今天来介绍几个简单的方法. 使用第三方服务 有这样的一种简单的方式,借助http://www.tianqi.com/plugin/网上的天气服务,可以定制我们的显示形状,实现添加天气预报的功能. 下面给出一个简单的小例子: 复制代码 代码如下: <iframe width="420" scrolling="no" height="60" frameborder=&

php 购物车完整实现代码

 1.商品展示页面   .代码如下: <table width="255"  border="0" cellspacing="0" cellpadding="0"> <tr> <td width="130" rowspan="6"><div align="center"> <?php         if(trim