linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP无限分类与树型论坛的实现方
·PHP生成静态页面的一些经验
·PHP入门速成
·入门级PHP程序员面试题
·vim的代码折叠
·php5学习笔记
·织梦CMS中文转换拼音函数研究
·PHP 和 MySQL初学入门
·文件下载统计php编程代码
·Windows下Apache+Tomcat+MySQL
·如何建立自己的新闻发布系统
·生成sessionid和随机密码的例子
·PHP初学者头疼问题总结
·使用无限生命期Session的方法
·Cookie及其使用详细介绍
最新文章
·BluePage通用分页类助开发者提
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
Google
 
PHP/MySQL 购物车
[ 作者:  加入时间:2008-02-14 14:18:12  来自:Linux联盟收集整理 ]
<?
  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. */
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息