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

时间: 2024-07-29 01:51:24

PHP/MySQL购物车代码的相关文章

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

这是加入购物车代码 点击按钮显示:未实现该方法或操作。是什么意思哪位大神看一下

问题描述 这是加入购物车代码 点击按钮显示:未实现该方法或操作.是什么意思哪位大神看一下 protected void btnShop_Click(object sender, EventArgs e) { string Orderid; if (Session["UserName"] == null) { Alert.AlertAndRedirect("您还没有登录,请登录后再购买,谢谢合作!", "Default.aspx"); } else

mysql-jsp链接Mysql数据库,代码问题、求指导

问题描述 jsp链接Mysql数据库,代码问题.求指导 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName(

php购物车代码

增加商品到购物车  代码如下 复制代码 <?php // // add_item.php: //  Add an item to the shopping cart. // session_start(); if (session_is_registered('cart')) {     session_register('cart'); } require 'lib.inc.php'; // LoadProducts() LoadProducts(); // Load products in

jQuery+HTML5加入购物车代码分享_jquery

这是一款基于jquery+html5实现的支持累加计价的网站购物车代码,可以把货物添加到购物车,添加物品数量,如果想取消购置某物品,这个功能也是可以实现的. 运行效果图:-----------------------------------查看效果----------------------------------- 为大家分享的jQuery+HTML5加入购物车代码如下 <head lang="en"> <meta charset="UTF-8"

PHP实现购物车代码[可重复使用]

php购物车的代码   发布者:[longlong16]  时间:[2006-11-1]     <?  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 {  fun

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

.net连接Mysql封装类代码 可直接调用

下面是我封装好的连接Mysql数据库的类,直接调用即可.   微软的visual studio没有自带连接Mysql的驱动,要去网上下载一个mysql-connector-net-6.4.3驱动,然后安装就可以使用. 下面是我封装好的连接数据库的类,直接调用即可. 复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; u

PHP处理SQL脚本文件导入到MySQL的代码实例

 通常在制作安装程式,数据备份程序的时候会要用到这样的代码,我看网上有是有不太多,而且有些也不是很好用,有时候这种代码直接用现成的可以节省很多时间,那么我就从stackoverflow转了一个过来,需要的朋友可以参考下 代码如下:<?php   // Name of the file $filename = 'churc.sql'; // MySQL host $mysql_host = 'localhost'; // MySQL username $mysql_username = 'root