首页 后端开发 php教程 关于php实现购物车的代码一

关于php实现购物车的代码一

Jun 19, 2018 pm 05:21 PM
php 购物车

本文介绍一个php实现的购物车代码,功能实现比较完整,实现了购物车、订货人信息、会员价等功能,还是不错的,有需要的朋友,可以参考下

1、商品展示页面

<table width="255"  border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" rowspan="6"><p align="center">
<?php
        if(trim($info[tupian]==""))
     {
       echo "暂无图片";
     }
     else
     {
?>
<img src="<?php echo $info[tupian];?>" width="130" height="100" border="0">
<?php
  }
?>
</p></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(&#39;请先登录后购物!&#39;);history.back();</script>";  
  exit;
 }
  $id=strval($_GET[id]);
$sql=mysql_query("select * from shangpin where id=&#39;".$id."&#39;",$conn);  
$info=mysql_fetch_array($sql);
if($info[shuliang]<=0)
 { 
   echo "<script>alert(&#39;该商品已经售完!&#39;);history.back();</script>";
   exit; 
 }
  $array=explode("@",$_SESSION[producelist]);
  for($i=0;$i<count($array)-1;$i++)
    {
  if($array[$i]==$id)
   {
      echo "<script>alert(&#39;该商品已经在您的购物车中!&#39;);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(&#39;请先登录,后购物!&#39;);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"><p align="center">
 <?php include("left.php");?>
    </p></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"><p align="center" style="color: #FFFFFF"><?php echo $_SESSION[username];?>的购物车</p></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=&#39;25&#39; colspan=&#39;6&#39; bgcolor=&#39;#FFFFFF&#39; align=&#39;center&#39;>您的购物车为空!</td>";
   echo"</tr>";
    }
     else
      {  
   ?>
<tr>
  <td width="125" height="25" bgcolor="#FFFFFF"><p align="center">商品名称</p></td>
  <td width="52" bgcolor="#FFFFFF"><p align="center">数量</p></td>
  <td width="64" bgcolor="#FFFFFF"><p align="center">市场价</p></td>
  <td width="64" bgcolor="#FFFFFF"><p align="center">会员价</p></td>
  <td width="51" bgcolor="#FFFFFF"><p align="center">折扣</p></td>
  <td width="66" bgcolor="#FFFFFF"><p align="center">小计</p></td>
  <td width="71" bgcolor="#FFFFFF"><p align="center">操作</p></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=&#39;".$id."&#39;",$conn);
       $info=mysql_fetch_array($sql);
       $total1=$num*$info[huiyuanjia];
       $total+=$total1;
       $_SESSION["total"]=$total;
   ?>
<tr>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[mingcheng];?></p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center">
      <input type="text" name="<?php echo $info[id];?>" size="2" class="inputcss" value=<?php echo $num;?>>
  </p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[shichangjia];?>元</p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[huiyuanjia];?>元</p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo @(ceil(($info[huiyuanjia]/$info[shichangjia])*100))."%";?></p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><?php echo $info[huiyuanjia]*$num."元";?></p></td>
  <td height="25" bgcolor="#FFFFFF"><p align="center"><a href="removegwc.php?id=<?php echo $info[id]?>">移除</a></p></td>
</tr>
<?php          
         }
     }
?>
<tr>
  <td height="25" colspan="8" bgcolor="#FFFFFF"><p align="right">
      <table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="125"><p align="center">
 <input type="submit" value="更改商品数量" class="buttoncss">
          </p></td>
          <td width="125"><p align="center"><a href="gouwu2.php">去收银台</a></p></td>
          <td width="125"><p align="center"><a href="gouwu1.php?qk=yes">清空购物车</a></p></td>
          <td width="125"><p align="left">总计:<?php echo $total;?></p></td>
        </tr>
      </table>
  </p></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"><p align="center">
 <?php include("left.php");?>
    </p></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"><p align="center" style="color: #FFFFFF">收货人信息</p></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"><p align="center">收货人姓名:</p></td>
 <td width="183" bgcolor="#FFFFFF"><p align="left"><input type="text" name="name" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;"></p></td>
 <td width="86" bgcolor="#FFFFFF"><p align="center">性别:</p></td>
 <td width="176" bgcolor="#FFFFFF"><p align="left">
 <select name="***">
  <option selected value="男">男</option>
  <option value="女">女</option>
 </select>
 </p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">详细地址:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input name="dz" type="text" class="inputcss" id="dz" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;" size="25">
 </p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">邮政编码:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="yb" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;"></p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">联系电话:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="tel" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;"></p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">电子邮箱:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left"><input type="text" name="email" size="25" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;"></p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">送货方式:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p 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>
</p></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><p align="center">支付方式:</p></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><p align="left">
 <select name="zfff" id="zfff">
  <option selected value="建设银行汇款">建设银行汇款</option>
  <option value="交通银行汇款">交通银行汇款</option>
  <option value="邮局汇款">邮局汇款</option>
  <option value="网上支付">网上支付</option>
 </select>
 </p></td>
 </tr>
 <tr>
 <td height="100" bgcolor="#FFFFFF"><p align="center">简单留言:</p></td>
 <td height="100" colspan="3" bgcolor="#FFFFFF"><p align="left">
 <textarea name="ly" cols="60" rows="8" class="inputcss" style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor=&#39;#ffffff&#39;" onMouseOut="this.style.backgroundColor=&#39;#e8f4ff&#39;"></textarea>
</p></td>
 </tr>
 <tr>
 <td height="25" colspan="4" bgcolor="#FFFFFF"><p align="center"><input type="submit" value="提交订单" class="buttoncss">
 </p></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=&#39;javascript&#39;>";
 echo" window.open(&#39;showdd.php?dd=&#39;+&#39;".$dd."&#39;,&#39;newframe&#39;,&#39;top=150,left=200,width=600,height=".$sum.",menubar=no,toolbar=no,location=no,scrollbars=no,status=no &#39;)";
 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实现手机端APP支付宝的支付功能

如何使用php实现快钱支付的功能

以上是关于php实现购物车的代码一的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

CakePHP 日志记录 CakePHP 日志记录 Sep 10, 2024 pm 05:26 PM

登录 CakePHP 是一项非常简单的任务。您只需使用一项功能即可。您可以记录任何后台进程(如 cronjob)的错误、异常、用户活动、用户采取的操作。在 CakePHP 中记录数据很容易。提供了 log() 函数

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

See all articles