• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php简略 购物车~ 学习笔记part1

php 搞代码 4年前 (2022-01-24) 22次浏览 已收录 0个评论

php简单 购物车~ 学习笔记part1
购物车 ,学习 ,part1

建表
wrox_shop_category 产品类别ID 和名称

create table wrox_shop_category (
category_id integer unsigned not null auto_increment,
category_name varchar(100) not null,
primary @本文来源gaodaimacom搞#代%码@网-搞代gaodaima码key (category_id)
)

ENGINE= InnoDB default character set latinl
collate latinl_general_cs;

商品类别表
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;

这两张表多指定InnoDB存储引擎,引用on delete cascade将类别删除后,商品也将自动删除.

代码文件

ShoppingCart类 ,该类如何编写,之后写shop.php和cart.php ,之后建立管理文件 生成商品数据库及商品条目

ShoppingCart,负责维护商品列表直到结帐。

包含属性:
contents,以数组形式返回购物车中的所有商品
isEmpty ,购物车是否是空
totalItems,不同种类的商品的总数量
totalQty 购物车中商品的总数量

methods :
construct()
addItem(item[,qty])
qtyItem(item)
removeItem(item)
removeAll()

商品列表,保存商品列表需要用到一个私有属性,初始化时候,将该属性初始化为一个空数组,之后每添加一个商品,此数组负责保存用产品ID索引的商品的数量???

<?php
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;
} //addItem行数将接收商品的Id,并把该值赋值于内部的items属性

public function qtyItem($item)
{
if (!isset($this->items[$item])
{
return 0;
}
else
{
return $this->items[$item];//返回了商品的数量
}
}

public function removeItem($item)
{
unset($this ->items[$item]);
}

public function removeAll()
{
$this->items = array();
}

}

?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php简略 购物车~ 学习笔记part1

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址