PHP/MySQL 购物车

时间:2010-01-13 08:35:50   来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:tion 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(...
  正文:

<?
  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;
来源:http://www.002pc.com/master/College/Programming/PHP/11792.html

收藏到:

关于《PHP/MySQL 购物车》文章的评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面

随机文章

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by rand() limit 10

顶出来的热门

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by diggtop desc,id desc limit 10
站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td