PHP Tutorials. Application Example 1

Source: Internet
Author: User
Tags count key mysql query return
Tutorials | Application Example Php/mysql SHOPPING cart Program
?
if (! $session &&! $scid) {
$session = MD5 (Uniqid (rand ());
Setcookie ("SCID", "$session", Time () + 14400);
}/* Last number are 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 the $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 section 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 ' is not NULL,
Category char (1) DEFAULT ' not NULL,
KEY ID (ID),
PRIMARY KEY (ID),
KEY 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 to put the 2 tables under in place of km Artshopper *
?>
/* Call functions like $cart->add_item and such, the Code. */



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.