首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP相关资源下载区 » [求助]关于用PHP实现网上购物的问题?请站长帮忙  


  作者:finett
  注册时间:2005-06-09
  主题/回复:4/8
  积分:428
  等级:★★☆(五级)
  称号:略有小成

用户联系方式已设置为保密

 

 发表:2005-07-29 23:09:33 阅读 2190 次 回复 4 次 得分2  |   字号 字色
[求助]关于用PHP实现网上购物的问题?请站长帮忙
[转帖]PHP/MySQL 购物车程序  
<? 
  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. */  

**************************************************************************************
请问站长,如何通过另一个文件调用以上的类(即:函数display_contents),以显示出某用户的购物清单?谢谢
 
 1#楼  
 
  回复人:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

   
 1#楼 发表于2005-08-05 23:08:32  评分:1 

程序比较长,没有一行一行看
使用类很简单。
在某页面中用include或require包含类文件,然后再页面中用 new关键字创建类的对象
 $aa=new myClass();
 
 $aa->display_contents();

 用对象名 $aa加上->即可调用类的成员和方法。
 2#楼  
 
  回复人:finett
  注册时间:2005-06-09
  主题/回复:4/8
  积分:428
  等级:★★☆(五级)
  称号:略有小成

用户联系方式已设置为保密
 2#楼 发表于2005-08-08 21:39:25  评分:× 

站长:
  你好!我本来也是按照你上面所述的代码来调用类的,但是调用的结果显示却为“Array”,就是说没有看到执行结果。因为函数display_contents()的返回值一个数组,我主要想问一下:怎样把这个数组的值分别陈列出来,即Array[0],Array[1],Array[2]......
 3#楼  
 
  回复人:finett
  注册时间:2005-06-09
  主题/回复:4/8
  积分:428
  等级:★★☆(五级)
  称号:略有小成

用户联系方式已设置为保密
 3#楼 发表于2005-08-09 01:53:54  评分:× 

续上:
    也就是说怎样使用foreach把数组的值分别遍历出来,分别显示各个值??
 4#楼  
 
  回复人:faallan
  注册时间:2005-11-23
  主题/回复:0/1
  积分:401
  等级:★★☆(五级)
  称号:略有小成

   
 4#楼 发表于2005-11-23 00:36:14  评分:1 

echo "<pre>";
print_r($rrr);
echo "</pre>";
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.017sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版