仓酷云

标题: PHP网站制作之PHP/MySQL 购物车 [打印本页]

作者: 变相怪杰    时间: 2015-2-4 00:25
标题: PHP网站制作之PHP/MySQL 购物车
熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作完整的网页,对元素属性达到熟悉程度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. */
  讲了这么多,无非是想说:学习PHP不仅要掌握方法,更多的是付出汗水,我不希望看到中途放弃的人,相信自己,相信自己的选择,更要相信自己的能力,如果自己想放弃,暴力一点的话,就自己抽自己一个嘴巴。
作者: 山那边是海    时间: 2015-2-4 13:05
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
作者: admin    时间: 2015-2-6 16:53
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
作者: 谁可相欹    时间: 2015-2-10 09:19
你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。
作者: 分手快乐    时间: 2015-2-12 18:40
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
作者: 小女巫    时间: 2015-2-22 19:57
做为1门年轻的语言,php一直很努力。
作者: 灵魂腐蚀    时间: 2015-3-6 09:32
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
作者: 愤怒的大鸟    时间: 2015-3-7 19:15
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
作者: 蒙在股里    时间: 2015-3-13 04:10
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
作者: 金色的骷髅    时间: 2015-3-17 07:12
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
作者: 柔情似水    时间: 2015-3-20 20:57
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
作者: 因胸联盟    时间: 2015-3-21 13:52
对于懒惰的朋友,我推荐php的集成环境xampp或者是wamp。这两个软件安装方便,使用简单。但是我还是强烈建议自己动手搭建开发环境。
作者: 小魔女    时间: 2015-3-22 17:09
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
作者: 爱飞    时间: 2015-3-24 17:54
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
作者: 第二个灵魂    时间: 2015-3-26 18:23
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
作者: 深爱那片海    时间: 2015-3-31 23:22
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
作者: 简单生活    时间: 2015-4-3 04:31
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
作者: 只想知道    时间: 2015-4-3 21:06
写的比较杂,因为我也是个新手,不当至于大家多多指正。
作者: 莫相离    时间: 2015-4-6 01:11
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。




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