- 先在修改的表单中列出会员级别【和添加时一样直接复制】
<tr>
<td class="label">会员价格:</td>
<td>
<?php foreach($mlData as $k=>$v): ?>
<?php echo $v[‘level_name‘] ;?> ¥<input type="text" name="member_price[<?php echo $v[‘id‘] ;?>]" value="" size="8"/>元<br>
<?php endforeach; ?>
</td>
</tr>
//取出所有会员的级别
$mlModel=D(‘member_level‘);
$mlData=$mlModel->select();
// 设置页面信息
$this->assign(array(
‘mlData‘=>$mlData,
‘brandData‘=> $brandData,
‘_page_title‘ => ‘修改商品‘,
‘_page_btn_name‘ => ‘商品列表‘,
‘_page_btn_link‘ => U(‘lst‘),
));
//取出这件商品已经设置好的会员价格
$mpModel=D(‘member_price‘);
$mpData=$mpModel->where(array(
‘goods_id‘ =>array(‘eq‘,$id),
))->select();
// 设置页面信息
$this->assign(array(
‘mlData‘=>$mlData,
‘mpData‘=> $mpData,
//取出所有会员的级别
$mlModel=D(‘member_level‘);
$mlData=$mlModel->select();
//取出这件商品已经设置好的会员价格
$mpModel=D(‘member_price‘);
$mpData=$mpModel->where(array(
‘goods_id‘ =>array(‘eq‘,$id),
))->select();
//把二维数组转一维: level_id=>price
$_mpData=array();
foreach($_mpData as $k => $v)
{
$_mpData[$v[‘level_id‘]]=$v[‘price‘];
}
// 设置页面信息
$this->assign(array(
‘mlData‘=>$mlData,
‘mpData‘=> $_mpData,
‘brandData‘=> $brandData,
‘_page_title‘ => ‘修改商品‘,
‘_page_btn_name‘ => ‘商品列表‘,
‘_page_btn_link‘ => U(‘lst‘),
));
<tr>
<td class="label">会员价格:</td>
<td>
<?php foreach($mlData as $k=>$v): ?>
<p>
<?php echo $v[‘level_name‘] ;?>
¥<input value="<?php echo $mpData[$v[‘id‘]] ; ?>" type="text" name="member_price[<?php echo $v[‘id‘] ;?>]" size="8"/>元
</p>
<?php endforeach; ?>
</td>
</tr>
- 提交表单之后把会员价格更新到会员价格表
思路:先清除原来的会员价格,把新的价格重新添加一遍就行。【代码和添加时一样,只在先删除原数据】
修改商品模型,把添加时的代码复制到修改的钩子方法:
protected function _before_update(&$data, $option)
{
$id = $option[‘where‘][‘id‘]; // 要修改的商品的ID
/**********处理会员价格 *******/
$mp=I(‘post.memebr_price‘);
$mpModel=D(‘member_price‘);
//先删除原来的会员价格
$mpModel->where(array(
‘goods_id‘=>array(‘eq‘,$id),
))->delete();
foreach($mp as $k =>$v)
{
$_v=(float)$v;
if($_v > 0)
{
$mpModel->add(array(
‘price‘=>$_v,
‘level_id‘=>$k,
‘goods_id‘=>$id,
));
}
}