Php shopping cart ~ Study notes part1-php tutorial

Source: Internet
Author: User
Php simple shopping cart ~ Learning notes part1 shopping cart, learning, part1 table wrox_shop_category product category ID and name createtablewrox_shop_category (category_idintegerunsignednotnullauto_increment, category_namevarchar ~ Study notes part1
Shopping cart, learning, part1


Create a table
Wrox_shop_category product category ID and name

Create table wrox_shop_category (
Category_id integer unsigned not null auto_increment,
Category_name varchar (100) not null,
Primary key (category_id)
)

ENGINE = InnoDB default character set latinl
Collate latinl_general_cs;

Item category table
Wrox_shop_inventory

Create table wrox_shop_inventory (
Item_id integer unsigned not null auto_increment,
Item_name varchar (100) not null,
Item_description text default '',
Price double (5, 2) not null,
Item_image varchar (255) not null,
Category_id integer unsigned not null,
Primary key (item_id ),
Foreign key (category_id)
References wrox_shop_category (category_id)
On delete cascade
)

Engine = InnoDB default character set latinl
Callate latinl_general_cs auto_increment = 0;


The two tables specify the InnoDB storage engine. after the on delete cascade is referenced to delete the category, the product is also automatically deleted.

Code file


ShoppingCart class, how to write this class, then write shop. php and cart. php, and then create a management file to generate a commodity database and item entries

ShoppingCart, responsible for maintaining the product list until the bill is settled.


Includes attributes:
Contents, returns all items in the shopping cart in array form
IsEmpty, whether the shopping cart is empty
TotalItems: total number of products of different types
Total number of items in the totalQty shopping cart

Methods:
Construct ()
AddItem (item [, qty])
QtyItem (item)
RemoveItem (item)
RemoveAll ()

A private attribute is required to save the product list. during initialization, this attribute is initialized as an empty array, and each time a product is added, this array stores the number of items indexed by the product ID ???


Class ShoppingCart
{
Private $ items;
Public function _ construct ()
{
$ This-> items = array ();
}

Public fuction _ get ($ value)
{
Switch ($ value)
{
Case 'Contents ':
Return $ this-> items;
Break;
Case 'isempty ':
Return (count ($ this-> items) = 0 );
Break;
Case 'totalitems ':
Return (count ($ this-> items ));
Break;

Case 'totalqty ':
Return array_sum ($ this-> items );
Break;

}
}

Public funciton addItem ($ item, $ qty = 1)
{
$ This-> items [$ item] = $ qty;
} // The number of addItem rows will receive the product Id and assign this value to the internal items attribute

Public function qtyItem ($ item)
{
If (! Isset ($ this-> items [$ item])
{
Return 0;
}
Else
{
Return $ this-> items [$ item]; // The number of items returned.
}
}

Public function removeItem ($ item)
{
Unset ($ this-> items [$ item]);
}

Public function removeAll ()
{
$ This-> items = array ();
}



}

?>

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.