仓酷云

标题: PHP网页设计PHP/MySQL 购物车法式 [打印本页]

作者: 若相依    时间: 2015-2-4 00:03
标题: PHP网页设计PHP/MySQL 购物车法式
既然选择了PHP,就要坚持学下去!大家有没有问自己为什么会选择学习PHP呢?就我个人而言,完全是因为兴趣,因为我的专业和计算机完全无关,但是就是对编程很赶兴趣,尤其对网络编程、web开发特别赶兴趣。   

     <?
  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. */
   
  
不过还好,PHP语言给出的语法错误很详细,只要稍微熟悉一点之后,看错误提示就能很容易找出错误所在的。PHP还有一个特别好用的调试功能,在PHP语句中,你可以随时用echo来输出结果。
作者: 变相怪杰    时间: 2015-2-4 08:23
对于懒惰的朋友,我推荐php的集成环境xampp或者是wamp。这两个软件安装方便,使用简单。但是我还是强烈建议自己动手搭建开发环境。
作者: 只想知道    时间: 2015-2-5 10:55
我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。
作者: 小妖女    时间: 2015-2-9 03:10
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
作者: 海妖    时间: 2015-2-9 22:05
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
作者: 金色的骷髅    时间: 2015-2-11 08:09
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
作者: 因胸联盟    时间: 2015-2-21 21:08
没接触过框架的人,也不用害怕,其实框架就是一种命名规范及插件,学会一个框架其余的框架都很好上手的。
作者: 深爱那片海    时间: 2015-3-6 21:39
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
作者: 灵魂腐蚀    时间: 2015-3-7 10:39
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
作者: 第二个灵魂    时间: 2015-3-13 04:08
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
作者: 分手快乐    时间: 2015-3-20 12:05
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
作者: 活着的死人    时间: 2015-3-31 11:45
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
作者: 飘灵儿    时间: 2015-4-2 19:33
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
作者: 仓酷云    时间: 2015-4-22 01:10
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
作者: 愤怒的大鸟    时间: 2015-5-4 16:27
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
作者: 不帅    时间: 2015-5-6 19:09
本文当是我的笔记啦,遇到的问题随时填充
作者: 蒙在股里    时间: 2015-6-4 19:40
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
作者: 飘飘悠悠    时间: 2015-6-15 11:15
兴趣是最好的老师,百度是最好的词典。
作者: 再现理想    时间: 2015-6-20 03:18
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
作者: 乐观    时间: 2015-7-12 05:44
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
作者: admin    时间: 2015-7-18 06:43
php里的数组为空的时候是不能拿来遍历的;(这个有点低级啊,不过我刚被这个边界问题墨迹了好长一会)




欢迎光临 仓酷云 (http://www.ckuyun.com/) Powered by Discuz! X3.2