目录
购物车
Shopping Cart
首页 php教程 php手册 一个简单的php+Ajax购物车程序代码(1/2)

一个简单的php+Ajax购物车程序代码(1/2)

May 25, 2016 pm 04:54 PM
foreach iframe include

本文章来给大家推荐一个不错的购物车效果,这里主要求包括了几个东西,一个是购物车类用php写的, 还有一个Ajax操作用到了jquery,还有一个jquery插件thickbox了,下面我们来看看。

购物车类:shop_cart.php
购物车的操作:cart_action.php
首页:index.html
Ajax操作用到了jquery,还有一个jquery插件thickbox

不多说了你可以先看看效果示例
shop_cart.php当然是购物车的核心,但是这个类很简单,因为他又引进了cart_action.php用于对外操作。所以这个类显得相当精简。
购物车类shop_cart.php

 代码如下 复制代码

cart_name = $name;
$this->items = $_SESSION[$this->cart_name];
}

/**
* setItemQuantity() - Set the quantity of an item.
*
* @param string $order_code The order code of the item.
* @param int $quantity The quantity.
*/
function setItemQuantity($order_code, $quantity) {
$this->items[$order_code] = $quantity;
}

/**
* getItemPrice() - Get the price of an item.
*
* @param string $order_code The order code of the item.
* @return int The price.
*/
function getItemPrice($order_code) {
// This is where the code taht retrieves prices
// goes. We'll just say everything costs $9.99 for this tutorial.
return 9.99;
}

/**
* getItemName() - Get the name of an item.
*
* @param string $order_code The order code of the item.
*/
function getItemName($order_code) {
// This is where the code that retrieves product names
// goes. We'll just return something generic for this tutorial.
return 'My Product (' . $order_code . ')';
}

/**
* getItems() - Get all items.
*
* @return array The items.
*/
function getItems() {
return $this->items;
}

/**
* hasItems() - Checks to see if there are items in the cart.
*
* @return bool True if there are items.
*/
function hasItems() {
return (bool) $this->items;
}

/**
* getItemQuantity() - Get the quantity of an item in the cart.
*
* @param string $order_code The order code.
* @return int The quantity.
*/
function getItemQuantity($order_code) {
return (int) $this->items[$order_code];
}

/**
* clean() - Cleanup the cart contents. If any items have a
*           quantity less than one, remove them.
*/
function clean() {
foreach ( $this->items as $order_code=>$quantity ) {
if ( $quantity items[$order_code]);
}
}

/**
* save() - Saves the cart to a session variable.
*/
function save() {
$this->clean();
$_SESSION[$this->cart_name] = $this->items;
}
}

?>

对于cart_action,他实现了shop_cart类与index的中间作用,用于更新,删除,增加商品的操作。
cart_action.php

 代码如下 复制代码

getItemQuantity($_GET['order_code'])+$_GET['quantity'];
$Cart->setItemQuantity($_GET['order_code'], $quantity);
}else{

if ( !empty($_GET['quantity']) ) {
foreach ( $_GET['quantity'] as $order_code=>$quantity){
$Cart->setItemQuantity($order_code, $quantity);
}
}

if ( !empty($_GET['remove']) ) {
foreach ( $_GET['remove'] as $order_code ) {
$Cart->setItemQuantity($order_code, 0);
}
}
}
$Cart->save();

header('Location: cart.php');

?>

还有就是index.html实现对外的操作,也就是添加操作

 代码如下 复制代码





Shopping Cart





return false;
});
});





还有就是cart.php这是我们的购物车

 代码如下 复制代码
include('shopping_cart.class.php');
session_start();
$Cart = new Shopping_Cart('shopping_cart');
?>




Shopping Cart







Shopping Cart


hasItems() ) : ?>










$total_price = $i = 0;
foreach ( $Cart->getItems() as $order_code=>$quantity ) :
$total_price += $quantity*$Cart->getItemPrice($order_code);
?>
" : ""; ?>









数量 商品名称 商品编号 单价 总价 删除
getItemName($order_code); ?> $getItemPrice($order_code); ?> $getItemPrice($order_code)*$quantity); ?>
您的消费总金额是:¥




您还没有购物.



加载简单的购物车





首页 1 2 末页

教程链接:

随意转载~但请保留教程地址★

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

我如何裁剪HTML中的IFrame? 我如何裁剪HTML中的IFrame? Aug 29, 2023 pm 04:33 PM

内联框架在HTML中称为iframe。标签指定内容中的一个矩形区域,浏览器可以在其中显示带有滚动条和边框的不同文档。要在当前HTML文档中嵌入另一个文档,请使用内联框架。可以使用HTMLiframe名称属性指定元素的引用。在JavaScript中,对元素的引用也是使用name属性进行的。iframe本质上用于在当前显示的网页中显示网页。包含iframe的文档的URL使用“src”属性指定。语法以下是HTML的语法<iframesrc="URL"title="d

iframe中的data-id是什么意思 iframe中的data-id是什么意思 Aug 28, 2023 pm 02:25 PM

iframe中的data-id是指在HTML标签中使用的自定义属性,用于存储特定元素的标识符。通过使用data-id属性,可以为iframe元素添加一个唯一的标识符,以便在JavaScript中对其进行操作和访问。data-id属性的命名可以根据具体的需求进行自定义,但通常会遵循一些命名规范,以确保其唯一性和易读性。data-id属性还可以用于识别和操作特定的iframe。

iframe为什么加载慢 iframe为什么加载慢 Aug 24, 2023 pm 05:51 PM

iframe加载慢的原因主要包括网络延迟、资源加载时间长、加载顺序、缓存机制以及安全策略等。详细介绍:1、网络延迟,当浏览器加载一个包含iframe的网页时,需要发送请求到服务器获取iframe中的内容,若网络延迟较高,那么获取内容的时间就会增加,从而导致iframe加载慢;2、资源加载时间长,资源的大小较大或者服务器响应时间较长时,加载速度会更加明显地变慢;3、加载顺序等等。

什么技术可以代替iframe 什么技术可以代替iframe Aug 24, 2023 pm 01:53 PM

可以代替iframe的技术有Ajax、JavaScript库或框架、Web组件技术、前端路由和服务器端渲染等。详细介绍:1、Ajax是一种用于创建动态网页的技术。它可以通过在后台与服务器进行数据交换,实现页面的异步更新,而无需刷新整个页面,使用Ajax可以更加灵活地加载和显示内容,不再需要使用iframe来嵌入其他页面;2、JavaScript库或框架,如React等等。

iframe有哪些加载事件 iframe有哪些加载事件 Aug 28, 2023 pm 01:55 PM

iframe的加载事件有onload事件、onreadystatechange事件、onbeforeunload事件、onerror事件、onabort事件等。详细说明:1、onload事件,指定加载iframe完成后要执行的JavaScript代码;2、onreadystatechange事件,指定当iframe状态变化时要执行的JavaScript代码等等。

iframe中的危险在哪里 iframe中的危险在哪里 Sep 08, 2023 pm 03:14 PM

iframe中的危险主要有:1、安全漏洞,恶意的网页可以通过iframe加载其他网页,并进行一些攻击行为;2、同源策略突破,通过在iframe中加载其他域名下的网页,能突破同源策略,实现跨域通信,这可能会被恶意攻击;3、代码执行问题,在iframe中加载的网页可以执行JS代码,这可能导致一些安全问题;4、SEO问题,搜索引擎可能无法正确解析和索引通过iframe加载的内容等等。

Python中iframe是什么意思 Python中iframe是什么意思 Aug 25, 2023 pm 03:24 PM

Python中iframe是一种HTML标签,用于在网页中嵌入另一个网页或文档。在Python中,可以使用各种库和框架来处理和操作iframe,其中最常用的是BeautifulSoup库,可以轻松地从一个网页中提取出iframe的内容,并对其进行操作和处理。掌握如何处理和操作iframe对于Web开发和数据抓取都是非常有用的。

监测iframe的滚动行为 监测iframe的滚动行为 Feb 18, 2024 pm 08:40 PM

如何监听一个iframe的滚动,需要具体代码示例当我们在网页中使用iframe标签嵌入其他网页时,有时候需要对iframe中的内容进行一些特定的操作。其中一个常见的需求是监听iframe的滚动事件,以便在滚动发生时执行相应的代码。以下将介绍如何使用JavaScript来监听一个iframe的滚动,并提供具体的代码示例供参考。获取iframe元素首先,我们需要

See all articles