php Shopping Cart Full Implementation Code

  • 2021-06-28 12:07:55
  • OfStack

1. Merchandise Display Page


<table width="255"  border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" rowspan="6"><div align="center">
<?php
        if(trim($info[tupian]==""))
     {
       echo " No pictures yet ";
     }
     else
     {
?>
<img src="<?php echo $info[tupian];?>" width="130" height="100" border="0">
<?php
  }
?>
</div></td>
  <td width="20" height="16">&nbsp;</td>
  <td width="113"><font color="EF9C3E"> [ <?php echo $info[mingcheng];?> ] </font></td>
 </tr>
 <tr>
  <td height="16">&nbsp;</td>
  <td><font color="910800"> [Market price: <?php echo $info[shichangjia];?> ] </font></td>
 </tr>
 <tr>
  <td height="16">&nbsp;</td>
  <td><font color="DD4679"> [Membership price: <?php echo $info[huiyuanjia];?> ] </font></td>
 </tr>
 <tr>
  <td height="16">&nbsp;</td>
  <td> [ <a href="lookinfo.php?id=<?php echo $info[id];?>"> see information </a> ] </td>
 </tr>
 <tr>
  <td height="16">&nbsp;</td>
  <td> [ <a href="addgouwuche.php?id=<?php echo $info[id];?>"> Put in shopping cart </a> ] </td>
 </tr>
 <tr>
  <td height="16">&nbsp;</td>
  <td><font color="13589B"> [Remaining quantity: 
      <?php  
      if(($info[shuliang]-$info[cishu])>0)
      {
         echo ($info[shuliang]-$info[cishu]);
      }
      else
      {
         echo " Out of Sale ";
      }
      ?> ] </font></td>
 </tr>
 </table>
     <?php
      }
     ?>     
</table>

2. File addgouwuche.php


<?php
session_start();
include("conn.php");

if($_SESSION[username]=="")
 {
  echo "<script>alert(' Please login before shopping !');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(' The goods have been sold out !');history.back();</script>";
   exit; 
 }
  $array=explode("@",$_SESSION[producelist]);
  for($i=0;$i<count($array)-1;$i++)
    {
  if($array[$i]==$id)
   {
      echo "<script>alert(' This item is already in your shopping cart !');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(' Please login before shopping !');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">&nbsp;</td>
    <td width="590" valign="top"><table width="550" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>&nbsp;</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];?> Shopping cart </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'> Your shopping cart is empty !</td>";
   echo"</tr>";
    }
     else
      {  
   ?>
<tr>
  <td width="125" height="25" bgcolor="#FFFFFF"><div align="center"> Commodity Name </div></td>
  <td width="52" bgcolor="#FFFFFF"><div align="center"> Number </div></td>
  <td width="64" bgcolor="#FFFFFF"><div align="center"> Market value </div></td>
  <td width="64" bgcolor="#FFFFFF"><div align="center"> Membership price </div></td>
  <td width="51" bgcolor="#FFFFFF"><div align="center"> Discount </div></td>
  <td width="66" bgcolor="#FFFFFF"><div align="center"> Subtotal </div></td>
  <td width="71" bgcolor="#FFFFFF"><div align="center"> operation </div></td>
</tr>
<?php
/**
 *  Shopping Cart   Commodity Quantity Management 
 * Edit www.ofstack.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];?> element </div></td>
  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info[huiyuanjia];?> element </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." element ";?></div></td>
  <td height="25" bgcolor="#FFFFFF"><div align="center"><a href="removegwc.php?id=<?php echo $info[id]?>"> remove </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=" Change commodity quantity " class="buttoncss">
          </div></td>
          <td width="125"><div align="center"><a href="gouwu2.php"> Go to the checkout </a></div></td>
          <td width="125"><div align="center"><a href="gouwu1.php?qk=yes"> empty cart </a></div></td>
          <td width="125"><div align="left"> Total: <?php echo $total;?></div></td>
        </tr>
      </table>
  </div></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"><div align="center">
 <?php include("left.php");?>
    </div></td>
    <td width="10" background="images/line2.gif">&nbsp;</td>
    <td width="590" valign="top"><table width="550" height="15" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>&nbsp;</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"> consignee </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">
/**
 *  Shopping Cart   consignee 
 * Edit www.ofstack.com
*/
function chkinput(form)
    {
      if(form.name.value=="")
       {
      alert(" Please enter the name of the consignee !");
      form.name.select();
      return(false);

    }
    if(form.dz.value=="")
       {
      alert(" Please enter the consignee address !");
      form.dz.select();
      return(false);

    }
    if(form.yb.value=="")
       {
      alert(" Please enter the consignee zip code !");
      form.yb.select();
      return(false);

    }
    if(form.tel.value=="")
       {
      alert(" Please enter the consignee's contact number !");
      form.tel.select();
      return(false);

    }
    if(form.email.value=="")
       {
      alert(" Please enter consignee E-mail address !");
      form.email.select();
      return(false);

    }
    if(form.email.value.indexOf("@")<0)
     {
        alert(" Consignee E-mail Address format input error !");
        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"> Name of consignee: </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"> Gender: </div></td>
 <td width="176" bgcolor="#FFFFFF"><div align="left">
 <select name="***">
  <option selected value=" male "> male </option>
  <option value=" female "> female </option>
 </select>
 </div></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><div align="center"> Detailed address: </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"> Postal Code: </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"> Contact number: </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"> Mail box: </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"> Delivery method: </div></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left">
 <select name="shff" id="shff">
  <option selected value=" Ordinary Postal Service "> Ordinary Postal Service </option>
  <option value=" Express Mail "> Express Mail </option>
  <option value=" Provide home delivery service "> Provide home delivery service </option>
  <option value=" Personal Delivery "> Personal Delivery </option>
  <option value="E-mail">E-mail</option>
      </select>
</div></td>
 </tr>
 <tr>
 <td height="25" bgcolor="#FFFFFF"><div align="center"> Payment method: </div></td>
 <td height="25" colspan="3" bgcolor="#FFFFFF"><div align="left">
 <select name="zfff" id="zfff">
  <option selected value=" Construction Bank Remittance "> Construction Bank Remittance </option>
  <option value=" Bank of Communications Remittance "> Bank of Communications Remittance </option>
  <option value=" Post Office Remittance "> Post Office Remittance </option>
  <option value=" Online payment "> Online payment </option>
 </select>
 </div></td>
 </tr>
 <tr>
 <td height="100" bgcolor="#FFFFFF"><div align="center"> Simple message: </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=" place order " 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. Database Profile conn.php


<?php
$conn=mysql_connect("localhost","root","") or die(" Database Server Connection Error ".mysql_error());
mysql_select_db("shop",$conn) or die(" Database Access Error ".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
?>


Related articles: