PHP Session Shopping Cart Class

Source: Internet
Author: User
Tags foreach array key key string php class php session query

 

<?php class cart{Public Function Cart () {if (!isset ($_session[' cart ')) {$_session[' cart '] = array (); }/* Add commodity param int $id Commodity primary key string $name commodity name float $price commodity price int $num Shopping quantity */Public function ad Ditem ($id, $name, $price, $num, $img) {//If the commodity already exists, add the quantity if (Isset ($_session[' cart '] [$id])) {$this->incnum ($id, $nu
			m);
		Return
		} $item = Array ();
		$item [' id '] = $id;
		$item [' name '] = $name;
		$item [' price '] = $price;
		$item [' num '] = $num;
		$item [' img '] = $img;
	$_session[' cart ' [$id] = $item; /* Modify the number of items in the shopping cart int $id primary KEY int $num The number of changes to a product, that is, to change the quantity of an item directly to $num/Public function Modnum ($id, $num =1) {if!
		Isset ($_session[' cart ' [$id])) {return false;
	$_session[' cart ' [$id] [' num '] = $num; }/* Product quantity +1/Public function Incnum ($id, $num =1) {if (Isset ($_session[' cart '] [$id])) {$_session[' cart '] [$id] [
		' num '] + = $num; }/* Commodity quantity-1/Public Function decnum ($id, $num =1) {if isset ($_session[' cart '] [$iD]) {$_session[' cart '] [$id] [' num ']-= $num;
		If the quantity is reduced to 0, delete the product if ($_session[' cart '] [$id] [' num '] <1) {$this->delitem ($id);
	}/* Delete commodity/Public Function Delitem ($id) {unset ($_session[' cart '] [$id]);
	/* Get a single product/Public function GetItem ($id) {return $_session[' cart '] [$id];
	} * * Query the type of merchandise in the cart/Public Function getcnt () {return count ($_session[' cart '));
		} * * Query the number of items in the cart/Public Function getnum () {if ($this->getcnt () = 0) {//0, number is 0 return 0;
		} $sum = 0;
		$data = $_session[' cart '];
		foreach ($data as $item) {$sum + = $item [' num '];
	return $sum;
		/* * The total amount of merchandise in the cart/Public Function GetPrice () {//The quantity is 0, the price is 0 if ($this->getcnt () = = 0) {return 0;
		} $price = 0.00;
		foreach ($this->items as $item) {$price + = $item [' num '] * $item [' price '];
	Return sprintf ("%01.2f", $price);
	/* Empty cart/Public Function clear () {$_session[' cart '] = 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.