1 public function goods_number() 2 { 3 // 接收商品ID 4 $id = I('get.id'); 5 $gnModel = D('goods_number'); 6 7 // 处理表单 8 if(IS_POST) 9 {
//var_dump($_POST)die;10 // 先删除原库存11 $gnModel->where(array(12 'goods_id' => array('eq', $id),13 ))->delete();14 //var_dump($_POST);die;15 $gaid = I('post.goods_attr_id');16 $gn = I('post.goods_number');17 // 先计算商品属性ID和库存量的比例18 $gaidCount = count($gaid);19 $gnCount = count($gn);20 $rate = $gaidCount/$gnCount;21 // 循环库存量22 $_i = 0; // 取第几个商品属性ID23 foreach ($gn as $k => $v)24 {25 $_goodsAttrId = array(); // 把下面取出来的ID放这里26 // 后来从商品属性ID数组中取出 $rate 个,循环一次取一个27 for($i=0; $i<$rate; $i++)28 {29 $_goodsAttrId[] = $gaid[$_i];30 $_i++;31 }32 // 先升序排列33 sort($_goodsAttrId, SORT_NUMERIC); // 以数字的形式排序34 // 把取出来的商品属性ID转化成字符串35 $_goodsAttrId = (string)implode(',', $_goodsAttrId);36 $gnModel->add(array(37 'goods_id' => $id,38 'goods_attr_id' => $_goodsAttrId,39 'goods_number' => $v,40 ));41 }42 $this->success('设置成功!', U('goods_number?id='.I('get.id')));43 exit;44 }45 46 // 根据商品ID取出这件商品所有可选属性的值47 $gaModel = D('goods_attr');48 $gaData = $gaModel->alias('a')49 ->field('a.*,b.attr_name')50 ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id=b.id')51 ->where(array(52 'a.goods_id' => array('eq', $id),53 'b.attr_type' => array('eq', '可选'),54 ))->select();55 // 处理这个二维数组:转化成三维:把属性相同的放到一起56 $_gaData = array();57 foreach ($gaData as $k => $v)58 {59 $_gaData[$v['attr_name']][] = $v;60 }61 62 // 先取出这件商品已经设置过的库存量63 $gnData = $gnModel->where(array(64 'goods_id' => $id,65 ))->select();66 //var_dump($gnData);67 68 $this->assign(array(69 'gaData' => $_gaData,70 'gnData' => $gnData,71 ));72 73 // 设置页面信息74 $this->assign(array(75 '_page_title' => '库存量',76 '_page_btn_name' => '返回列表',77 '_page_btn_link' => U('lst'),78 ));79 // 1.显示表单80 $this->display();81 }