感谢唠叨老大支持
可能是个人习惯问题,通常我都不愿意在类中夹杂html元素,原因是因为如果使用模板来处理的话,就显得有点不伦不类,而且做美工比较困难,要改页面还得去更改类.
当然现在看来表单部分的处理过于复杂,应当是有办法精减的.
对于表单元素的处理个人看法还是放到类之外,这样便于数据检查,如果放到类之中,那么又得加上一堆数据检查的代码.个人拙见.csdn的页面我从来就没有排整齐过,很多代码行都有一定程度的错位,在myblog.boei.cn我把这个类重新排了一下版.
可能是个人习惯问题,通常我都不愿意在类中夹杂html元素,原因是因为如果使用模板来处理的话,就显得有点不伦不类,而且做美工比较困难,要改页面还得去更改类.
当然现在看来表单部分的处理过于复杂,应当是有办法精减的.
对于表单元素的处理个人看法还是放到类之外,这样便于数据检查,如果放到类之中,那么又得加上一堆数据检查的代码.个人拙见.csdn的页面我从来就没有排整齐过,很多代码行都有一定程度的错位,在myblog.boei.cn我把这个类重新排了一下版.
<?php
/**************************************************************************
Copyright (C), 2004, ustb
文件名: testcart.php
作 者: [偶然]
说 明: 测试购物车
版 本: 1.0
日 期: 2004-8-19
历 史:
<作者> <时间> <版本> <说明>
[偶然] 2004-8-19 1.0 创建模块
**************************************************************************/
session_start();
require_once "cart.class.php";
$cart = new cart();// 添加
if(isset($_POST['action'])&&!empty($_POST['action'])&&$_POST['action']=="additem")
{
$postitempid = isset($_POST['itempid'])&&!empty($_POST['itempid'])?$_POST['itempid']:null;
$postitemname = isset($_POST['itemname'])&&!empty($_POST['itemname'])?$_POST['itemname']:null;
$postitemprice = isset($_POST['itemprice'])&&!empty($_POST['itemprice'])?$_POST['itemprice']:null;
$postitemnum = isset($_POST['itemnum'])&&!empty($_POST['itemnum'])?$_POST['itemnum']:null;
$arr = array(
"$postitempid" =>array(
"name" => $postitemname,
"price" => $postitemprice,
"num" => $postitemnum
)
);
$cart->addItem($arr);
}
// 删除
if(isset($_GET['act'])&&!empty($_GET['act'])&&$_GET['act']=="del"&&!empty($_GET['id']))
{
$cart->emptyItem($_GET['id']);
}
// 清空
if(isset($_GET['act'])&&!empty($_GET['act'])&&$_GET['act']=="clean")
{
$cart->emptyAll();
header("location: testcart.php");
}// 改
if(isset($_POST['action'])&&$_POST['action']=="修改")
{
print_r($_POST);
$postpnum = isset($_POST['pnum'])&&!empty($_POST['pnum'])?$_POST['pnum']:null;
if(!empty($postpnum))
{
foreach($postpnum as $key=>$val)
{
$cart->modItem($key,$val[0]) ;
}
}
}
?>
<a href="?act=clean">清空</a>
<table border=1 width="700" align="center">
<form name="form_cart" method="post" action="">
<?php
// 取得
$getarr = $cart->getItems();
if(is_array($getarr)&&!empty($getarr))
{
// 列表
foreach($getarr as $pkey=>$pval)
{
if($pkey!=null)
{
?>
<tr>
<td>ID:<?=$pkey?></td>
<td>名称:<?=$pval['name']?></td>
<td>单价:<?=$pval['price']?></td> <td>数量:<input type="text" name="pnum[<?=$pkey?>][]" value="<?=$pval['num']?>"></td>
<td>合计<?=$pval['count']?></td>
<td><a href="?act=del&id=<?=$pkey?>">删除</a></td>
</tr>
<?php
}
}
}
?>
<tr>
<td colspan="6"><input type="submit" name="action" value="修改">总计:<?=$cart->sum()?></td>
</tr>
</form>
</table>
因为只做测试,所以不对数量,ID等参数做严格限制,爱填什么就填什么.
<form name="form_add" method=post action="">
ID:<input type="text" name="itempid">
名称:<input type="text" name="itemname">
单价:<input type="text" name="itemprice">
数量:<input type="text" name="itemnum" value="">
<input type="hidden" name="action" value="additem">
<input type="submit">
</form>
主要是对修改数量作了修改,以前的办法太笨,现在可能聪明了一点.
{
Return true;
}
else
{
Return false;
}
}
http://myblog.boei.cn/demo/cart/testcart.php
大家都来测测,看能报出些什么错来.
{
echo "有";
}
else
{
echo "没有";
}
如果我是商家,我可能更愿意在特殊商品的编号上下功夫,因为特殊商品不会只有一个,所以我可能会对其编号加上"t"之类的前缀,这样更容易管理商品.
话说回来,对于像0,null,之类的边界值一向都是程序中最易出现矛盾的地方,感谢pswdf的提醒.
以下给出修改之后的类文件<?php
/**
* 类名: cart
* 描述: 尝试以最简单的方式实现购物车
* 其他: 2004-8-19
*/
class cart
{
/**
* 函数名称: addItem
* 函数功能: 添加商品
* 输入参数: $data ------------- 商品数组
* 函数返回值: none
* 其它说明: 因为数据是记录在session中,所以不用返回
*/
function addItem($data)
{
if(is_array($data)&&!empty($data))
{
foreach($data as $key=>$val)
{
// 如果商品存在就加数量和价格
if($this->_isExists($key))
{
$_SESSION['cart'][$key]["count"] += floatval($val['price'])*intval($val['num']);
$_SESSION['cart'][$key]["num"] += intval($val['num']);
}
// 否则直接加入
else
{
$_SESSION['cart'][$key] = $data[$key];
$_SESSION['cart'][$key]["name"] = $val['name'];
$_SESSION['cart'][$key]["price"] = $val['price'];
$_SESSION['cart'][$key]["count"] = floatval($val['price'])*intval($val['num']);
$_SESSION['cart'][$key]["num"] = intval($val['num']);
}
}
}
} /**
* 函数名称: _isExists
* 函数功能: 判断此商品是否存在
* 输入参数: $id ---------- 商品ID
* 函数返回值: bool
* 其他说明: 2004-8-19
*/
function _isExists($id)
{
if(isset($_SESSION['cart'][$id])&&!empty($_SESSION['cart'][$id])&&array_key_exists($id,$_SESSION['cart']))
{
Return true;
}
else
{
Return false;
}
} /**
* 函数名称: modItem
* 函数功能: 修改商品数量
* 输入参数: $id -------------- 商品ID
* $num ------------- 商品数量
* 函数返回值: 返回值说明
* 其他说明: 说明
*/
function modItem($id,$num)
{
$arr = $this->getItems($id);
// 如果商品存在就改数量和价格
if($this->_isExists($id))
{
$_SESSION['cart'][$id]["count"] = intval($arr['price'])*intval($num);
$_SESSION['cart'][$id]["num"] = intval($num);
}
} /**
* 函数名称: getItems
* 函数功能: 取得商品数组
* 输入参数: $id --------------- 某商品的ID
* 函数返回值: array
* 其它说明: 2004-8-19
*/
function getItems($id=null)
{
if(isset($_SESSION['cart']))
{
if($id==null)
{
Return $_SESSION['cart'];
}
else
{
Return $_SESSION['cart'][$id];
}
}
} /**
* 函数名称: emptyItem
* 函数功能: 删除商品
* 输入参数: $id ----------- 商品ID
* 函数返回值: bool
* 其它说明: 2004-8-19
*/
function emptyItem($id=null)
{
if($id==null)
{
unset($_SESSION['cart']);
}
else
{
unset($_SESSION['cart'][$id]);
}
} /**
* 函数名称: sum
* 函数功能: 统计总价
* 输入参数: none
* 函数返回值: int
* 其它说明: 2004-8-19
*/
function sum()
{
$total = 0;
if(isset($_SESSION['cart'])&&!empty($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $key=>$val)
{
$total += $val['count'];
}
}
Return $total;
}
}
?>
function modItem($id,$num)
{
$arr = $this->getItems($id);
// 如果商品存在就改数量和价格
if($this->_isExists($id))
{
$_SESSION['cart'][$id]["count"] = floatval($arr['price'])*intval($num);
$_SESSION['cart'][$id]["num"] = intval($num);
}
}
我还是基本同意ustb(偶然)的观点.
我也做了一个购物车类,把html放到了类中.但是当页面调整的时候很麻烦.
所以我提倡把html放到类外.
购物,OA,html生成。呵呵。
不过好像还是用到了->,sigh。
我那个模板文件在这里
当要想要购物的页面一定要这几行然后用get方式传给card.php文件如:card.php?action=add&title=www&pier=10.0。
session_start();
if(!isset($_SESSION['user']))
{
session_register('user');
$_SESSION['user'][0]=getenv("REMOTE_ADDR");
$_SESSION['user'][1]=time();
}card.htm
-----------------------------------
<table width="360" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#006699">
<tr bgcolor="#99CCFF" valign="middle">
<TD width='30%' height="18" align="center" class=GR>商品名称</td>
<td width="15%" align="center" class=GR>单价</td>
<td width="20%" align="center" class=GR>数量</td>
<td width="15%" align="center" class=GR>金额</td>
<td width="20%" align="center" class=GR>删除</td>
</tr>
<!--BEGIN PROUDUCT-->
<tr bgcolor="#C1CA9D" >
<td height="18" class=GR>{name_card}</td>
<td class=GR>{pier_card}</td>
<td class=GR>{num_card}</td>
<td class=GR>{count_card}</td>
<td class=GR>{delete_card}</td>
</tr>
<!--END PRODUCT-->
</table>
Warning: Cannot use a scalar value as an array in cart.class.php on line 48 清空
这里还有误。
我看看把我那个文件改成类。
您好,看了你写的blog,很不错,想和您请教一下在线编辑器的程序,行吗?
谢谢。已经加您QQ。
我的QQ是:17120023
Msn: [email protected]谢谢!