PHP+MySQL 购物车程序实例

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

时间: 2024-07-31 08:21:25

PHP+MySQL 购物车程序实例的相关文章

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 che

php实现的MySQL通用查询程序_php实例

if(get_magic_quotes_gpc()==1){    ?> <html> <head><title>MySQL通用查询程序</title></head> <body> 注意本程序需要将PHP配置文件(PHP3为php3.ini,PHP4为php.ini)中的magic_quotes_gpc 设成Off或0,修改后请重新启动Apache. </body> </html>    <?   

使用MongoDB和JSP实现一个简单的购物车系统实例_JSP编程

本文介绍了JSP编程技术实现一个简单的购物车程序,具体如下: 1 问题描述 利用JSP编程技术实现一个简单的购物车程序,具体要求如下. (1)用JSP编写一个登录页面,登录信息中有用户名和密码,分别用两个按钮来提交和重置登录信息. (2)编写一个JSP程序来获取用户提交的登录信息并查询数据库,如果用户名为本小组成员的名字且密码为对应的学号时,采用JSP内置对象的方法跳转到订购页面(显示店中商品的种类和单价等目录信息):否则采用JSP动作提示用户重新登录(注:此页面上要包含前面的登录界面). (3

实用的php购物车程序

实用的php教程购物车程序 以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下. <?php //调用实例 require_once 'cart.class.php'; session_start(); if(!isset($_SESSION['cart'])) {  $_SESSION['cart'] = new Cart; } $cart =& $_SESSION['cart']; if( ($_SERVER['REQUEST_METHOD']=="P

会员数据导入uchome程序实例

会员数据导入uchome程序实例 <?php try {  $uc_db = new DataSource(UC_DBHOST , UC_DBUSER , UC_DBPW , UC_DBNAME, 'mysql', true );  $uc_query = new DbQueryForMysql($uc_db); } catch (DbException $e) {  // 数据库出错处理处  exit('Database support needed'); } $sql ="select

使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表

c++|程序 概述 本文档列出了 Crystal Decisions 技术支持网站上所有可用的,使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表.本文档还给出了每一个程序的描述和下载链接.随着新程序加入我们的支持站点,本文档将不断更新.---------------------------------- 目录 VISUAL C# .NET 一.数据库连通性 1.csharp_web_simplelogonengine.exe 2.csharp_web_simplelogonvi

PHP和MySQL存储过程的实例演示

以下的文章主要是向大家介绍的是PHP和MySQL存储过程的实例演示,我前两天在相关网站看见PHP和MySQL存储过程的实例演示的资料,觉得挺好,就拿出来供大家分享.希望在大家今后的学习中会有所帮助.   PHP与MySQL存储过程 实例一:无参的存储过程     $conn = MySQL_connect('localhost','root','root') or die ("数据连接错误!!!"); MySQL_select_db('test',$conn); $sql = &quo

PHP/MySQL 购物车

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($tab

MySQL查询优化程序

    4.2 MySQL查询优化程序    在发布一个选择行的查询时, MySQL进行分析,看是否能够对它进行优化,使它执行更快.本节中,我们将研究查询优化程序怎样工作.更详细的信息,可参阅MySQL参考指南中的"Getting Maximum Performance from MySQL",该章描述了MySQL采用的各种优化措施.该章中的信息会不断变化,因为MySQL的开发者不断对优化程序进行改进,因此,有必要经常拜访一下该章,看看是否有可供利用的新技巧.(http://www.m