PHP.40-TP Framework Mall Application Example-backstage 17-product attribute 3-modification and deletion of commodity classification

Source: Internet
Author: User

modification of commodity classification

1, Change the form goods/edit.html, Add the dropdown box

2, because the commodity property modification involves the commodity attribute table goods_attr{id,attr_value,attr_id,goods_id} and the attribute table attribute{id,attr_name, attr_option_values,type_id}, and considering that the modified table should also be displayed when a type adds a property , the product attribute table should be connected according to the property sheet Remove related data {all attributes of the same type, the item has an existing attribute value}

The data output of the even table query is as Follows:

3. use PHP to display the display output in the form edit.html

<!--product properties--<table style="display:none;"Width="90%" class="tab_table"align="Center"> <tr ><td>Product Type:<?php Buildselect ('Type','type_id','ID','type_name', $data ['type_id']);?> </td></tr> <tr> <td><ul id="attr_list"> <!--loop All original attribute values--<?PHP $attrId= Array ();//all attribute IDs that have occurred                    foreach($gaData as$k =$v)://Judging if this property ID first appears is + otherwise is-                            if(in_array ($v ['attr_id'], $ATTRID)) $opt='-'; Else{$opt='+'; $attrId []= $v ['attr_id']; }                     ?> <li> <input type="Hidden"Name="goods_attr_id[]"Value="<?php echo $v [' ID '];?>"/> <?phpif($v ['Attr_type'] =='options available'):?> <a onclick="addnewattr (this);"href="#">[<?php echo $opt;?>]</a> <?php endif;?> < ? php echo $v ['Attr_name'];?> :                             <?phpif($v ['attr_option_values']): $attr= Explode (',', $v ['attr_option_values']); ?> <SelectName="attr_value[<?php echo $v [' attr_id '];? >][]"> <option value=""> Please select </option> <?phpforeach($attr as$k 1 =$v 1):if($v 1 = = $v ['Attr_value'])                                                $Select='selected= "selected"'; Else                                                 $Select="'; ?> <option <?php Echo $Select;?> value="<?php echo $v 1;?>"><?php echo $v 1;?></option> <?php endforeach;?> </Select> <?phpElse:?> <input type="text"Name="attr_value[<?php echo $v [' attr_id '];? >][]"Value="<?php echo $v [' attr_value '];?>"/> <?php endif?> </li> <?php endf oreach,?> </ul></td> </tr> </table>
Commodity attribute Sheet output

note: in the same attribute, the first one should appear as the + sign, The other is the number

4. Data modification

Train of Thought: because after the function of inventory quantity need to use the ID of the commodity attribute, if the empty product attribute is added again, the ID is changed, resulting in the inventory amount of data, will result in: each time the product changes, the inventory amount of data will be invalid need to re-add, so you can not follow the previous method, Remove the original data and update it all

So there are three different cases

1. Add a new attribute insert

2. Modify the original property update

3. Delete Attributes: use Ajax to delete

4.1 Add hidden fields to the form, submit the ID of the product attribute

  

  4.2 Modify the commodity model Goodsmodel.class.php/_before_update (), loop through each attribute value, look for a product attribute id, if there is a modification; if not, add :

/********** Modifying commodity properties **********/$gaid= I ('post.goods_attr_id'); $attrValue= I ('Post.attr_value'); $gaModel= M ('goods_attr'); $_i=0;//Number of Cycles            foreach($attrValue as$k =$v) {                foreach($v as$k 1 =$v 1) {                    //this replace into can achieve the same function//Replace Into: If the record exists, it is modified, and the record does not exist to add. Determine if a record exists in the primary key field//$gaModel->execute (' REPLACE into p39_goods_attr VALUES ("'. $gaid [$_i]. '", "'. $v 1. '", "'. $k. '", "'. $id. ') '); //Find out if this attribute value has an ID                    if($gaid [$_i] = ="') $gaModel-Add (array ('goods_id'=$id,'attr_id'=$k,'Attr_value'=$v 1,)); Else$gaModel-where(array ('ID'= = Array ('eq', $gaid [$_i] )))->setfield ('Attr_value', $v 1); $_i++; }            }
Handling Commodity Attributes

note:Replace into usage

Replace into is similar to insert, except that replace into first attempts to insert data into the table, 1. If you find that this row of data is already in the table (judging by primary KEY or unique Index)

Delete the row data first, and then insert the new Data. 2. otherwise, Insert the new data directly.

Note that the table in which the data is inserted must have a primary key or a unique index! otherwise, Replace into will insert the data directly, which will result in duplicate data appearing in the Table.

4.3 Ajax removal

Modify the "-" number of the JS code, incoming Product ID

The Add method in the commodity controller handles this request:

note: find_in_set (str,strlist):

STR string to query for
Strlist field name parameters are separated by "," as (1,2,6,8)
Query field (strlist) contains the result of (str) , the result is null or the record

  However: This function is a full table scan and cannot be optimized! So if the amount of data in the table is very large, and this query uses very frequently do not use this function!

PHP.40-TP Framework Mall Application Example-backstage 17-product attribute 3-modification and deletion of commodity classification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.