加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php购物车示例

发布时间:2022-02-16 10:24:47 所属栏目:PHP教程 来源:互联网
导读:这里又提供一款php购物车实例代码,这是一款适合种位要开发商城或购物系统参考的开发实例了,告诉你如果增加商品到购物并删除,与购物车的数据库设计实例. inventory表,代码如下: create table inventory ( product tinytext not null, quantity tinytext not
  这里又提供一款php购物车实例代码,这是一款适合种位要开发商城或购物系统参考的开发实例了,告诉你如果增加商品到购物并删除,与购物车的数据库设计实例.
 
  inventory表,代码如下:
 
  create table inventory (  
    product tinytext not null,  
    quantity tinytext not null,  
    id int(4) default '0' not null auto_increment,  
    description tinytext not null,  
    price float(10,2) default '0.00' not null,  
    category char(1) default '' not null,  
    key id (id),  
    primary key (id),  
    key price (price)  
  );
  insert into inventory values ('硬盘','5','1','80g','5600','1');
  insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
  insert into inventory values ('dvd-rom','7','3','12x','2000','1');
  insert into inventory values ('主板','3','4','asus','5000','2');
  insert into inventory values ('显示卡','6','5','64m','4500','1');
  insert into inventory values ('刻录机','4','6','52w','3000','1');
  shoping表
 
  create table shopping (  
    session tinytext not null,  
    product tinytext not null,  
    quantity tinytext not null,  
    card tinytext not null,  
    id int(4) default '0' not null auto_increment,  
    key id (id),  
    primary key (id)  
  );
  shoper表
 
  create database shopper;
  use shopper;
  create table shopping (  
    session tinytext not null,  
    product tinytext not null,  
    quantity tinytext not null,  
    card tinytext not null,  
    id int(4) default '0' not null auto_increment,  
    key id (id),  
    primary key (id)  
  );  
  create table inventory (  
    product tinytext not null,  
    quantity tinytext not null,  
    id int(4) default '0' not null auto_increment,  
    description tinytext not null,  
    price float(10,2) default '0.00' not null,  
    category char(1) default '' not null,  
    key id (id),  
    primary key (id),  
    key price (price)  
  );
  insert into inventory values ('硬盘','5','1','80g','5600','1');
  insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
  insert into inventory values ('dvd-rom','7','3','12x','2000','1');
  insert into inventory values ('主板','3','4','asus','5000','2');
  insert into inventory values ('显示卡','6','5','64m','4500','1');
  insert into inventory values ('刻录机','4','6','52w','3000','1');
  main.php购物页面,代码如下:
 
  /*mysql --user=root --password=your_password */
  
  
  include("shoppingcart.php");  
  $cart = new cart;
  $table="shopping";
  
  /* 查询并显示所有存货表中的信息 */
      $query = "select * from inventory";
      $invresult = mysql_query($query);  
      if (!($invresult)) {  
         echo "查询失败<br>";
         exit;
      }  
      echo "以下产品可供订购∶";
      echo "<table border=0>";
      echo "<tr><td bgcolor=#aaccff>产品编号</td><td bgcolor=#aaccff>产品名称</td><td bgcolor=#aaccff>单价</td>";
      echo "<td bgcolor=#aaccff>剩余数量</td><td bgcolor=#aaccff>产品描述</td><td bgcolor=#aaccff>放入购物车</td></tr>";
      while($row_inventory = mysql_fetch_object($invresult)) {
      echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";  
      echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";  
      echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";  
      echo "<td bgcolor=#aaccff>".$row_inventory->quantity."</td>";  
      echo "<td bgcolor=#aaccff>".$row_inventory->description."</td>";
      echo "<td bgcolor=#aaccff><a href='additem.php?product=".$row_inventory->product."'><img border='0' src='cart.gif' width='81' height='17'></a></td></tr>";  
      }
      echo "</table>";
      echo "<br>购物车中产品的数量∶".$cart->quant_items($table, $session);
      echo "<br><br><a href='clearcart.php'><img border='0' src='car.gif'></a>清空购物车";
  
  
   //shoppingcart.php代码
  
   <?php
      if (!$session && !$s) {  
          $s = md5(uniqid(rand()));  
          setcookie("session", "$s", time() + 14400);  
      }
  
  /* 检查是否有 seesion, 如果没有产生一个 md5 的唯一 id, 并利用 cookie 存入 $s 中。
  并且设置其存在时间为 14400 sec 也就是 4 小时 */
  
  
      $mysql_link = mysql_connect("127.0.0.1", "root", "test");  
      if (!($mysql_link)) {  
         echo "连接数据库失败<br>";
         exit;
      }  
      $mysql_select=mysql_select_db("shopper", $mysql_link);
      if (!($mysql_select)) {  
         echo "打开数据库失败<br>";
         exit;
      }  
               }  
              $numrows = mysql_num_rows($result);  
              if($numrows == 0) {  
                  return 0;  
              } else {  
                  $row = mysql_fetch_object($result);  
                  return $row->quantity;  
              }  
          }
  
          function add_item($table, $session, $product, $quantity) {  
              $qty = $this->check_item($table, $session, $product);  
              if($qty == 0) {  
                  $query = "insert into $table (session, product, quantity) values ";  
                  $query .= "('$session', '$product', '$quantity') ";  
                  mysql_query($query);  
              } else {  
                  $quantity += $qty;  
                  $query = "update $table set quantity='$quantity' where session='$session' and ";  
                  $query .= "product='$product' ";  
                  mysql_query($query);
              }  
          }  
            
          function delete_item($table, $session, $product) {  
              $query = "delete from $table where session='$session' and product='$product' ";  
              mysql_query($query);  
          }  
            
          function modify_quantity($table, $session, $product, $quantity) {  
              $query = "update $table set quantity='$quantity' where session='$session' ";  
              $query .= "and product='$product' ";  
              mysql_query($query);  
          }  
            
          function clear_cart($table, $session) {  
              $query = "delete from $table where session='$session' ";  
              mysql_query($query);  
          }  
            
          function cart_total($table, $session) {  
              $query = "select * from $table where session='$session' ";  
              $result = mysql_query($query);  
              if(mysql_num_rows($result) > 0) {  
                  while($row = mysql_fetch_object($result)) {  
                      $query = "select price from inventory where product='$row->product' ";  
                      $invresult = mysql_query($query);  
                      $row_price = mysql_fetch_object($invresult);  
                      $total += ($row_price->price * $row->quantity);  
                  }  
              }  
              return $total;  
          }  
            

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读