doushao8421 2018-12-18 18:46
浏览 71

PHP为了序列化字段值而创建类

So, I've got the basics of PHP now, and I'm writing a plugin to extend Woocommerce. I've added a custom tab to my product settings, and inside the tab I can add a dynamic number of Custom Charges relating to the product. Essentially, I use JQuery to create a div with 4 select elements inside. I can add any number of div elements (thus, any number of Custom Charges for the product). I've found a way to do this, but I believe there must be a better way (currently I use a 'counter' variable and give each div and select a numeric suffix). I say better way, because when I delete a div (thus delete a 'Custom Charge') I find I'm having to renumber all the existing div and select elements (otherwise my update_post_meta save process will fail.

This is how I'm saving the data currently:

//Additional Charge Types
$i = $_POST['count_of_charge_types']; <-this is a hidden field
$counter = 1;
if ($i>0){
    while($counter <= $i){
        $add_charge_sub = array("Description" => 
esc_attr($_POST['_add_charge_desc_'. $counter]),"Charge_Type" => 
esc_attr($_POST['_add_charge_type_'. $counter]),"Charge_Value" => 
esc_attr($_POST['_add_charge_value_'. $counter]),"Charge_Method" => 
esc_attr($_POST['_add_charge_method_'. $counter]));
        $add_charges[]=$add_charge_sub;
        $counter++;
};

update_post_meta($post_id,'_product_add_charges',$add_charges);

This is an example of the HTML so you can understand the naming of the elements and how they get saved:

<div id="add_charges_1" class="options_group">

<div id="add_charges_header_1" class="desc_header">
   <button id="but_remove_charge_type_1" type="button">Remove Charge Type</button>

  <input type="text" class="short" style="" name="_add_charge_desc_1" 
  id="_add_charge_desc_1" value="Fuel Surcharge"> 
</div>

<input type="text" class="short" style="" name="_add_charge_value_1" id="_add_charge_value_1" value="4"> 

<select id="_add_charge_method_1" name="_add_charge_method_1">
            <option value="unit" selected="selected">Per Unit</option>
            <option value="group">Per Product Group</option>        
</select>

</div>

it works fine, but as I said, when I delete a 'Custom Charge' I might be left with a gap in the numbering of the elements which throws my loops off.

Can anyone point me in the right direction on how to generate the elements in such a way that I can do away with the numbering system and simply serialize the fields.

I've read something about PHP serialization here, and it talks about creating classes. Creating classes is new to me, and I have no idea where to start and whether it's the correct and easiest approach anyway.

The reason I'm looking at classes is because as you can see fromt he update_post_meta code I'm creating arrays and storing them in the DB. I can't just loop over each item in the form using foreach($_POST as $name => $value) because I need the options to stay together in the arrays.

I have another motive for trying to work this out though, and that's because the next stage of development of the site will involve creating a lot of custom options, and I want to make the development quick and efficient.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?