搜尋
首頁php教程php手册PHP/MySQL 购物车程序

PHP/MySQL 购物车程序

Jun 21, 2016 am 09:02 AM
nbspproductquerysessiontable

 

    
  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. */
  
  



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用