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