php 购物车实例(申精)_PHP
if(! $session && ! $scid) {
/*
session用来区别每一个购物车,相当于每个车的身份证号;
scid只用来标识一个购物车id号,可以看做是每个车的名字;
当该购物车的id和session值两者都不存在时,就产生一个新购物车
*/
$session = md5(uniqid(rand()));
/*
产生一个唯一的购物车session号
rand()先产生个随机数,uniqid()再在该随机数的基础上产生一个独一无二的字符串,最后对该字符串进行md5
*/
SetCookie(scid, $session, time() + 14400);
/*
设置该购物车cookie
变量名:scid(不知到这里是不是少了一个 $号呢?=》更正:scid要加“”)
变量值: $session
有效时间:当前时间+14400秒(4小时内)
关于setcookie函数的详细用法,大家还是参看php手册吧~
*/
}
class Cart { //开始购物车类
function check_item( $table, $session, $product) {
/*
查验物品(表名,session,物品)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
看一看'表'里该'购物车'中有没有该'产品'
即,该产品有没有已经放入购物车
*/
$result = mysql_query( $query);
if(! $result) {
return 0;
}
/*
查询失败
*/
$numRows = mysql_num_rows( $result);
if( $numRows == 0) {
return 0;
/*
若没有找到,则返回0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
若找到,则返回该物品数量
这里有必要解释一下mysql_fetch_object函数(下面还会用到):
【mysql_fetch_object() 和 mysql_fetch_array() 类似,只有一点区别 - 返回一个对象而不是数组。】
上面这句话摘自php手册,说得应该很明白了吧~
简单的说就是,取一条记录中的某个字段,应该用“->”而不是像数组一样用下标
*/
}
}
function add_item( $table, $session, $product, $quantity) {
/*
添加新物品(表名,session,物品,数量)
*/
$qty = $this->check_item( $table, $session, $product);
/*
调用上面那个函数,先检查该类物品有没有已经放入车中
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*若车中没有,则像车中添加该物品*/
} else {
$quantity += $qty; //若有,则在原有基础上增加数量
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
$query .= product=' $product' ;
mysql_query( $query);
/*
并修改数据库
*/
}
}
function delete_item( $table, $session, $product) {
/*
删除物品(表名,session,物品)
*/
$query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
mysql_query( $query);
/*
删除该购物车中该类物品
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
修改物品数量(表名,session,物品,数量)
*/
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
将该物品数量修改为参数中的值
*/
}
function clear_cart( $table, $session) {
/*
清空购物车(没什么好说)
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
车中物品总价
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
/*
先把车中所有物品取出
*/
if(mysql_num_rows( $result) > 0) {
while( $row = mysql_fetch_object( $result)) {
/*
如果物品数量>0个,则逐个判断价格并计算
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
从inventory(库存)表中查找该物品的价格
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
总价 += 该物品价格 * 该物品数量
( 大家应该能看明白吧 )
*/
}
}
return $total; //返回总价钱
}
function display_contents( $table, $session) {
/*
获取关于车中所有物品的详细信息
*/
$count = 0;
/*
物品数量计数
注意,该变量不仅仅为了对物品数量进行统计,更重要的是,它将作为返回值数组中的下标,用来区别每一个物品!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
先取出车中所有物品
*/
while( $row = mysql_fetch_object( $result)) {
/*
分别对每一个物品进行取详细信息
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
从inventory(库存)表中查找该物品的相关信息
*/
$row_inventory = mysql_fetch_object( $result_inv);
$contents[product][ $count] = $row_inventory->product;
$contents[price][ $count] = $row_inventory->price;
$contents[quantity][ $count] = $row->quantity;
$contents[total][ $count] = ( $row_inventory->price * $row->quantity);
$contents[description][ $count] = $row_inventory->description;
/*
把所有关于该物品的详细信息放入 $contents数组
$contents是一个二维数组
第一组下标是区别每个物品各个不同的信息(如物品名,价钱,数量等等)
第二组下标是区别不同的物品(这就是前面定义的 $count变量的作用)
*/
$count++; //物品数量加一(即下一个物品)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
同时调用上面那个cart_total函数,计算下总价钱
并放入 $contents数组中
*/
return $contents;
/*
将该数组返回
*/
}
function num_items( $table, $session) {
/*
返回物品种类总数(也就是说,两个相同的东西算一种 好像是废话- -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
取出车中所有物品,获取该操作影响的数据库行数,即物品总数(没什么好说的)
*/
}
function quant_items( $table, $session) {
/*
返回所有物品总数(也就是说,两个相同的东西也算两个物品 - -#)
*/
$quant = 0;// 物品总量
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
把每种物品逐个取出
*/
$quant += $row->quantity; //该物品数量加到总量里去
}
return $quant; //返回总量
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
