php购物车实现方法
本文实例讲述了php购物车实现方法。分享给大家供大家参考。具体分析如下:
这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容.
增加商品到购物车,代码如下:
<?php
//
//add_item.php:
// Addanitemtotheshoppingcart.
//
session_start();
if(session_is_registered('cart')){
session_register('cart');
}
require'lib.inc.php';//LoadProducts()
LoadProducts();//Loadproductsin$master_products_list
//Make$curr_productglobal
$curr_product=array();
//Loopthroughalltheproductsandpulluptheproduct
//thatweareinterestedin
foreach($master_products_listas$prod_id=>$product){
if(trim($prod_id)==trim($_GET[id])){
$curr_product=$product;
}
}
//Registeroursession
//session_register('cart');
//if(session_is_registered('cart'))echo"已经注册";
if($_POST[ordered]){ //Iftheyhavechosentheproduct
array_push($_SESSION[cart][products],array(trim($_POST[id]),$_POST[quantity]));
$_SESSION[cart][num_items]+=$_POST[quantity];
}
?>
<html>
<head>
<title>
<?phpif($_POST[ordered]){ ?>
已经添加<?phpecho$curr_product[name];?>到您的购物篮
<?php}else{ ?>
添加<?phpecho$curr_product[name];?>到您的购物篮
<?php}?>
</title>
</head>
<body>
<?phpif($_POST[ordered]){ ?>
<h1><?phpecho$curr_product[name];?>
添加至购物篮成功</h1>
<ahref="cart.php">返回</a>商品列表页面.
<?php} else{ ?>
<h1>添加<?phpecho$curr_product[name];?>到您的购物篮</h1>
<formaction="<?phpecho$PHP_SELF;?>"method="post">
商品名称:<?phpecho$curr_product[name];?>
<br>
商品说明:<?phpecho$curr_product[desc];?>
<br>
商品单价:RMB<?phpecho$curr_product[price];?>
<br>
商品数量:<inputtype="text"size="7"name="quantity">
<inputtype="hidden"name="id"value="<?phpecho$_GET[id];?>">
<inputtype="hidden"name="ordered"value="1">
<inputtype="submit"value="添加至购物栏">
</form>
<?php}?>
</body>
</html>
查看购物车的商品,代码如下:
<?php
//
//cart.php:
//
session_start();
require'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if(session_is_registered('cart')){
session_register('cart');
}
//如果购物篮没有初始化,则初始化购物篮
if(!isset($_SESSION[cart][num_items])){
$_SESSION[cart]=array("num_items"=>0,
"products" =>array());
}
//Fromsite_lib.inc,Loadsthe$master_products_listarray
LoadProducts();//载入物品列表
?>
<html>
<head>
<title>演示会话跟踪的购物篮程序</title>
</head>
<body>
<h1>欢迎进入网上商店</h1>
<?php
if($_SESSION[cart][num_items]){ //Ifthereissomethingtoshow
?>
<h2>当前在购物篮里的物品</h2>
<br>
<tableborder="2"cellpadding="5"cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
数量
</th>
<th>
</th>
</tr>
<?php
//Loopthroughtheproducts
foreach($_SESSION[cart][products]as$i=>$product){
$product_id=$product[0];
$quantity =$product[1];
$total+=$quantity*
(double)$master_products_list[$product_id][price];
?>
<tr>
<td>
<?phpecho$master_products_list[$product_id][name];?>
</td>
<td>
<?phpecho$master_products_list[$product_id][desc];?>
</td>
<td>
<?phpecho$master_products_list[$product_id][price];?>
</td>
<td>
<formaction="change_quant.php"method="post">
<inputtype="hidden"name="id"value="<?phpecho$i;?>">
<inputtype="text"size="3"name="quantity"
value="<?phpecho$quantity;?>">
</td>
<td>
<inputtype="submit"value="数量更改">
</form>
</td>
</tr>
<?php
}
?>
<tr>
<tdcolspan="2"ALIGN="right">
<b>合计:</b>
</td>
<tdcolspan="2">
RMB:<?phpecho$total;?>
</td>
<td> </td>
</tr>
</table>
<br>
<br>
<?php
}
?>
<h2>商店待出售的商品</h2>
<br>
<i>
我们提供以下商品待售:
</i>
<br>
<tableborder="2"cellpadding="5"cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
</th>
</tr>
<?php
//Showalloftheproducts
foreach($master_products_listas$product_id=>$item){
?>
<tr>
<td>
<?phpecho$item[name];?>
</td>
<td>
<?phpecho$item[desc];?>
</td>
<td>
$<?phpecho$item[price];?>
</td>
<td>
<ahref="add_item.php?id=<?phpecho$product_id;?>">
添加至购物篮
</a>
</td>
</tr>
<?php
}
?>
</table>
修改购物车的数量,代码如下:
<?php
//
//change_quant.php:
// Changethequantityofanitemintheshoppingcart.
//
session_start();
if(session_is_registered('cart')){
session_register('cart');
}
//Typecasttoint,makingsureweaccessthe
//rightelementbelow
$i=(int)$_POST[id];
//Savetheoldnumberofproductsfordisplay
//andarithmetic
$old_num=$_SESSION[cart][products][$i][1];
if($_POST[quantity]){
$_SESSION[cart][products][$i][1]=$_POST[quantity];//changethequantity
}else{
unset($_SESSION[cart][products][$i]);//Sendtheproductintooblivion
}
//Updatethenumberofitems
$_SESSION[cart][num_items]=($old_num>$_POST[quantity])?
$_SESSION[cart][num_items]-($old_num-$_POST[quantity]):
$_SESSION[cart][num_items]+($_POST[quantity]-$old_num);
?>
<html>
<head>
<title>
数量修改
</title>
</head>
<body>
<h1>将数量:<?phpecho$old_num;?>更改为
<?phpecho$_POST[quantity];?></h1>
<ahref="cart.php">返回</a>商品列表页面.
</body>
</html>
功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:
<?php
//物品数组
$master_products_list=array();
//载入物品数据函数
functionLoadProducts(){
global$master_products_list;
$filename='products.txt';
$fp=@fopen($filename,"r")
ordie("打开$filename文件失败");
@flock($fp,1)
ordie("锁定$filename文件失败");
//读取文件内容
while($line=fgets($fp,1024)){
list($id,$name,$desc,$price)=explode('|',$line);//读取每行数据,数据以|格开
$id=trim($id);//去掉首尾特殊符号
$master_products_list[$id]=array("name"=> $name,//名称
"desc"=> $desc,//说明
"price"=>$price);//单价
}
@fclose($fp) //关闭文件
ordie("关闭$filename文件失败");
}
?>很简单,我们只用了4个文件就实现用php做好购物车功能,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好.
希望本文所述对大家的php程序设计有所帮助。