찾다
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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기