波利亚早在《怎样解题》提出,你是否知道一个与此有关的问题?一个类比的问题?
事实上我们编程也一样,我们在写功能代码时,发现代码功能相似性非常高,先抄后改就很有必要。
第一步 对比商品添加代码视图层,修改商品修改的视图层
add.html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ECSHOP 管理中心 - 添加新商品 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="__PUBLIC__/Admin/Styles/general.css" rel="stylesheet" type="text/css" />
<link href="__PUBLIC__/Admin/Styles/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>
<span class="action-span"><a href="__GROUP__/Goods/goodsList">商品列表</a>
</span>
<span class="action-span1"><a href="__GROUP__">ECSHOP 管理中心</a></span>
<span id="search_id" class="action-span1"> - 添加新商品 </span>
<div style="clear:both"></div>
</h1>
<div class="tab-div">
<div id="tabbar-div">
<p>
<span class="tab-front" id="general-tab">通用信息</span>
</p>
</div>
<div id="tabbody-div">
<form enctype="multipart/form-data" action="__SELF__" method="post">
<input type="hidden" name="id" value="<?php echo I('get.id');?>"/>
<table width="90%" id="general-table" align="center">
<tr>
<td class="label">商品名称:</td>
<td><input type="text" name="goods_name" size="60" />
<span class="require-field">*</span></td>
</tr>
<tr>
<td class="label">LOGO:</td>
<td><input type="file" name="logo" size="60" /></td>
</tr>
<tr>
<td class="label">市场售价:</td>
<td>
<input type="text" name="market_price" value="0" size="20" />
<span class="require-field">*</span>
</td>
</tr>
<tr>
<td class="label">本店售价:</td>
<td>
<input type="text" name="shop_price" value="0" size="20"/>
<span class="require-field">*</span>
</td>
</tr>
<tr>
<td class="label">是否上架:</td>
<td>
<input type="radio" name="is_on_sale" value="是" checked="checked" /> 是
<input type="radio" name="is_on_sale" value="否" /> 否
</td>
</tr>
<tr>
<td class="label">商品描述:</td>
<td>
<textarea id="goods_desc" name="goods_desc"></textarea>
</td>
</tr>
</table>
<div class="button-div">
<input type="submit" value=" 确定 " class="button"/>
<input type="reset" value=" 重置 " class="button" />
</div>
</form>
</div>
</div>
<div id="footer">
共执行 9 个查询,用时 0.025161 秒,Gzip 已禁用,内存占用 3.258 MB<br />
版权所有 © 2005-2012 上海商派网络科技有限公司,并保留所有权利。</div>
</body>
</html>
<!--导入在线编辑器 -->
<link href="__PUBLIC__/umeditor1_2_2-utf8-php/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="__PUBLIC__/umeditor1_2_2-utf8-php/third-party/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/umeditor1_2_2-utf8-php/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/umeditor1_2_2-utf8-php/umeditor.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/umeditor1_2_2-utf8-php/lang/zh-cn/zh-cn.js"></script>
<script>
UM.getEditor('goods_desc', {
initialFrameWidth : "100%",
initialFrameHeight : 350
});
</script>
edit.html代码:
照抄无误。
第二步 复制控制器的add方法,改为
public function edit(){
$id=I('get.id');
if(IS_POST){
$model =D('goods');
if( $model->create(I('post.'),2)) {
if(FALSE!==$model->add()){
//插入到数据库
$this->success('操作成功',U('lst'));
exit;
//http://localhost:8989/php/TpShop/Admin/goods/add
}
}
$error=$model->getError();
$this->error($error);
}
//显示表单
$this->display();
}
第三步 修改模型
//修改时调用create方法允许接收的字段
protected $updateFields='id,goods_name,market_price,shop_price,is_on_sale,goods_desc';