萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 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")
copyright © 萬盛學電腦網 all rights reserved