PHP语言之PHPMySQL购物车程序实例
小标 2018-09-14 来源 : 阅读 1680 评论 0

摘要:本文主要向大家介绍了PHP语言之PHPMySQL购物车程序实例,通过具体的内容向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言之PHPMySQL购物车程序实例,通过具体的内容向大家展示,希望对大家学习php语言有所帮助。

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 */ 
?>

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved