国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

PHP/MySQL 購物車程序

2024-07-24 12:56:55
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

 

     <?
  if(!$session && !$scid) {
  $session = md5(uniqid(rand()));
  setcookie("scid", "$session", time() + 14400);
  } /* last number is expiration time in seconds, 14400 sec = 4 hrs */
  
  class cart {
  function check_item($table, $session, $product) {
  $query = "select * from $table where session='$session' and product='$product' ";
  $result = mysql_query($query);
  
  if(!$result) {
  return 0;
  }
  
  $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;
  }
  
  function display_contents($table, $session) {
  $count = 0;
  $query = "select * from $table where session='$session' order by id ";
  $result = mysql_query($query);
  while($row = mysql_fetch_object($result)) {
  $query = "select * from inventory where product='$row->product' ";
  $result_inv = mysql_query($query);
  $row_inventory = mysql_fetch_object($result_inv);
  $contents["product"][$count] = $row_inventory->product;
  $contents["price"][$count] = $row_inventory->price;
  $contents["quantity"][$count] = $row->quantity;
  $contents["total"][$count] = ($row_inventory->price * $row->quantity);
  $contents["description"][$count] = $row_inventory->description;
  $count++;
  }
  $total = $this->cart_total($table, $session);
  $contents["final"] = $total;
  return $contents;
  }
  
  function num_items($table, $session) {
  $query = "select * from $table where session='$session' ";
  $result = mysql_query($query);
  $num_rows = mysql_num_rows($result);
  return $num_rows;
  }
  
  function quant_items($table, $session) {
  $quant = 0;
  $query = "select * from $table where session='$session' ";
  $result = mysql_query($query);
  while($row = mysql_fetch_object($result)) {
  $quant += $row->quantity;
  }
  return $quant;
  }
  }
  ?>
  
  /*
  this part contains a description of how to create the tables on your mysql server.
  
  # mysql dump 6.0
  #
  # host: localhost database: kmartshopper
  #--------------------------------------------------------
  # server version 3.22.25
  
  #
  # table structure for table '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)
  );
  
  #
  # table structure for table 'shopping'
  #
  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)
  );
  */
  
  example
  <?
  include("shoppingcart.php");
  $cart = new cart;
  $mysql_link = mysql_connect("localhost", "wwwrun", "");
  $mysql_select_db("kmartshopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartshopper */
  ?>
  /* call functions like $cart->add_item and such, see the code. */
  
  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄大仙区| 玛纳斯县| 蒙城县| 浠水县| 布尔津县| 台中市| 宁都县| 玛多县| 宁津县| 潼南县| 项城市| 台中县| 公主岭市| 通道| 邛崃市| 肇东市| 大埔区| 邵东县| 湾仔区| 陇南市| 石狮市| 古浪县| 阿尔山市| 竹溪县| 阳山县| 巧家县| 西乌| 喀喇沁旗| 苍南县| 宁津县| 鸡泽县| 巢湖市| 盖州市| 称多县| 慈溪市| 贵德县| 韩城市| 怀来县| 海盐县| 康保县| 八宿县|