search
HomeBackend DevelopmentPHP TutorialCode 1 about implementing shopping cart in php
Code 1 about implementing shopping cart in phpJun 19, 2018 pm 05:21 PM
phpshopping cart

This article introduces a shopping cart code implemented in PHP. The function implementation is relatively complete. It implements functions such as shopping cart, orderer information, membership price, etc. It is still good. Friends in need can refer to it

1. Product display page

<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="/static/imghwm/default1.png"  data-src="<?php echo $info[tupian];? alt="Code 1 about implementing shopping cart in php" >"  class="lazy"   width="130"    style="max-width:90%" 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. File 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. File 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. File 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, database configuration file 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");
?>

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website !

Related recommendations:

Use PHP to implement the payment function of Alipay on the mobile APP

How to use PHP to implement quick money payment Function

The above is the detailed content of Code 1 about implementing shopping cart in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version