search
Homephp教程php手册php session 购物车类

 

<?php class Cart{
	public function Cart() {
		if(!isset($_SESSION[&#39;cart&#39;])){
			$_SESSION[&#39;cart&#39;] = array();
		}
	}

	/*
	添加商品
	param int $id 商品主键
		  string $name 商品名称
		  float $price 商品价格
		  int $num 购物数量
	*/
	public  function addItem($id,$name,$price,$num,$img) {
		//如果该商品已存在则直接加其数量
		if (isset($_SESSION[&#39;cart&#39;][$id])) {
			$this->incNum($id,$num);
			return;
		}
		$item = array();
		$item['id'] = $id;
		$item['name'] = $name;
		$item['price'] = $price;
		$item['num'] = $num;
		$item['img'] = $img;
		$_SESSION['cart'][$id] = $item;
	}

	/*
	修改购物车中的商品数量
	int $id 商品主键
	int $num 某商品修改后的数量,即直接把某商品
	的数量改为$num
	*/
	public function modNum($id,$num=1) {
		if (!isset($_SESSION['cart'][$id])) {
			return false;
		}
		$_SESSION['cart'][$id]['num'] = $num;
	}

	/*
	商品数量+1
	*/
	public function incNum($id,$num=1) {
		if (isset($_SESSION['cart'][$id])) {
			$_SESSION['cart'][$id]['num'] += $num;
		}
	}

	/*
	商品数量-1
	*/
	public function decNum($id,$num=1) {
		if (isset($_SESSION['cart'][$id])) {
			$_SESSION['cart'][$id]['num'] -= $num;
		}

		//如果减少后,数量为0,则把这个商品删掉
		if ($_SESSION['cart'][$id]['num'] delItem($id);
		}
	}

	/*
	删除商品
	*/
	public function delItem($id) {
		unset($_SESSION['cart'][$id]);
	}
	
	/*
	获取单个商品
	*/
	public function getItem($id) {
		return $_SESSION['cart'][$id];
	}

	/*
	查询购物车中商品的种类
	*/
	public function getCnt() {
		return count($_SESSION['cart']);
	}
	
	/*
	查询购物车中商品的个数
	*/
	public function getNum(){
		if ($this->getCnt() == 0) {
			//种数为0,个数也为0
			return 0;
		}

		$sum = 0;
		$data = $_SESSION['cart'];
		foreach ($data as $item) {
			$sum += $item['num'];
		}
		return $sum;
	}

	/*
	购物车中商品的总金额
	*/
	public function getPrice() {
		//数量为0,价钱为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);
	}

	/*
	清空购物车
	*/
	public function clear() {
		$_SESSION['cart'] = array();
	}
}



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use