Library Inventory Management
Idea: Set inventory for each multi-select property of a product!! To assign multiple-choice attribute permutations, specify the inventory amount separately!!
The effect is as follows: "Determined by the attributes added by the product"
1. Build table goods_number{goods_id,goods_number,goods_str_id "Commodity attribute ID"}
if exists p39_goods_number;create table P39_goods_number ( null comment ' Product ID ', Nulldefault ' 0 ' comment ' inventory ', goods_attr_id varchar (null comment ' Commodity attribute Table id{if there is more than one, stitching into a string is saved in this field} ', key goods_id (goods_id) enginedefault Charset=utf8 Comment ' Inventory amount ';
Inventory table (Goods_number)
2 . Remove the attribute value from the controller GoodsController.class.php and output it to the form
Note: A: the two-dimensional array is converted to three-dimensional when the product property is recycled
B: When the stock is added , the constraint: Front background, id must be sorted in ascending order. Back to the background in ascending order, the same time the foreground query is also sorted before checking. to avoid property inversion when the foreground calls data
C: When modified, delete the original inventory amount, re-add
//Handling Inventory Volumes Public functionGoods_number () {//Receive item ID $id= I (' get.id '); $gnModel= M (' Goods_number '); //working with Forms if(is_post) {//first delete the original inventory, and then add, implementation changes $gnModel->where (Array( ' goods_id ' =Array(' eq ',$id), ))-Delete (); $gaid= I (' post.goods_attr_id '); $gnum= I (' Post.goods_number '); //statistic quantity, calculate proportion $cgaid=Count($gaid); $cgnum=Count($gnum); $rate=$cgaid/$cgnum; //Cycle Stock Volume $_i= 0; foreach($gnum as $k=$v) { $_goodsattrid=Array(); //take $rate from the array of commodity attribute IDs, looping through one at a time for($i= 0;$i<$rate;$i++) { $_goodsattrid[] =$gaid[$_i]; $_i++; } //Ascending order Sort($_goodsattrid, sort_numeric);//Sort by number//convert the extracted product attribute ID into a string $_goodsattrid= (string)implode(‘,‘,$_goodsattrid); $gnModel->add (Array( ' goods_id ' =$id, ' goods_attr_id ' =$_goodsattrid, ' goods_number ' =$v, )); } } //(stock revision) First take out the inventory that has been set up for this item $gnData=$gnModel->where (Array( ' goods_id ' =$id, ))-Select (); //Take out this item according to the Product ID value of all attributes and the name of the property in the attribute table based on the attribute ID $gaModel= M (' goods_attr '); $gaData=$gaModel->field (' a.*, B.attr_name, B.attr_type ') ->alias (' a ') -Join(' left JOIN __attribute__ b on a.attr_id = b.ID ') ->where (Array( ' goods_id ' =Array(' eq ',$id), ' b.attr_type ' =Array(' eq ', ' optional '), ))-Select (); //process $gadata data, convert two-dimensional to three-dimensional arrays: classify attributes identically $_gadata=Array(); foreach($gaData as $k=$v) { $_gadata[$v[' Attr_name '] [] =$v;//use attribute name as subscript } //var_dump ($_gadata);d ie; $this->assign (Array( ' Gndata ' =$gnData, ' gadata ' =$_gadata, ' _page_title ' = ' inventory management ', ' _page_btn_name ' = ' return to list ', ' _page_btn_link ' = ' + U ' (' l St '), )); //Show Form $this-display (); }
3. Display in form goods_number.html
Note: A: JS is invalid, debugging the following error: Uncaught referenceerror: $ is not defined (anonymous function)
Workaround: Import the jquery class library
B: cycle through the inventory in the form page
<layout name= "Layout"/><!--inventory List--><divclass= "List-div" id= "Listdiv" ><form method= "POST" action= "__self__" > <table cellpadding= "3" cellspacing= "1" > <tr> <!--Loop Output Properties-<?phpforeach($gaData as $k=$v):?> <th><?phpEcho $k;?></th> <?phpEndforeach;?> <th width= "> Stock </th> <th width=" > Actions </th> </tr> <?phpif($gnData):?> <?phpforeach($gnData as $k 0=$v 0):?> <trclass= "Tron" > <?PHP$gaCount=Count($gaData); foreach($gaData as $k=$v):?> <td> <select name= "goods_attr_id[]" > <option value= "" > Please select </option> <?phpforeach($v as $k 1=$v 1):$_attr=Explode(‘,‘,$v 0[' goods_attr_id ']); if(In_array($v 1[' ID '],$_attr)) $select= ' selected= ' selected "'; Else $select= ' '; ?> <option <?phpEcho $select;?> value= "<?php Echo$v 1[' id '];?> "><?phpEcho $v 1[' Attr_value '];?></option> <?phpEndforeach;?> </select> </td> <?phpEndforeach;?> <td><input type= "text" Name= "goods_number[]" value= "<?php Echo$v 0[' Goods_number '];?> "/></td> <td><input onclick=" ADDNEWTR (this); "type=" button "value = "<?php Echo$k 0==0? ' + ': '-';?> '/></td> </tr> <?phpEndforeach;?> <?phpElse:?> <trclass= "Tron" > <?PHP$gaCount=Count($gaData); foreach($gaData as $k=$v):?> <td> <select name= "goods_attr_id[]" > <option value= "" > Please select </option> <?phpforeach($v as $k 1=$v 1):?> <option value= "<?php Echo$v 1[' id '];?> "><?phpEcho $v 1[' Attr_value '];?></option> <?phpEndforeach;?> </select> </td> <?phpEndforeach;?> <td><input type= "text" name= "goods_number[" "value=" "/></td> < Td><input onclick= "ADDNEWTR (this);" type= "button" value= "+"/></td> </tr> <?phpendif;?> <tr id= "Submit" > <td align= "center" colspan= "<?php Echo$gaCount+2;?> "><input type=" Submit "value=" Submission "/></td> </tr> </table></form></di V><script type= "Text/javascript" src= "__pub__/umeditor1.2.3-utf8-php/third-party/jquery.min.js" ></ Script><script type= "Text/javascript" >functionADDNEWTR (btn) {varTR = $ (BTN). Parent ().parent (); if($ (BTN). val () = = "+") { varNEWTR = tr.Clone(); NEWTR. Find (": Button"). Val ("-"); $("#submit").before (NEWTR); } ElseTR.Remove (); }</script><script src= "__pub__/admin/js/tron.js" ></script>
4, in the model class GoodsModel.class.php Processing delete = "Delete the product, the stock amount is deleted
PHP.43-TP Framework Mall Application Example-backstage 16-Product Properties 3-Inventory management