...
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; // 取第幾個商品屬性ID 23 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 }