将可重复字段选项保存到PHP对象数组


Saving Repeatable Fields Options to PHP Object Array

因此,我尝试使用$_POST将一组动态创建的字段保存到数据库中。基本上,我不知道如何将选项保存为每个字段集的对象,而不是选项本身的数组。我很难描述。。。让我解释一下。

首先,这个东西的屏幕截图。

字段通过jQuery动态创建,效果非常好。以下是我为表单设置的字段:

<label>Calendar Name
    <input name="name[]" id="name[]" type="text" value="<?php echo $calendars['name'][$key]; ?>">
</label>
<label>Public URL
    <input name="url[]" id="url[]" type="text" value="<?php echo $calendars['url'][$key]; ?>">
</label>
<label>Color
    <input name="color[]" id="color[]" type="text" value="<?php echo $calendars['color'][$key]; ?>">
</label>

这与这个问题类似,但我想我不明白我应该如何使用JS正确地增加和排序索引值,或者如果有PHP解决方案,我完全没有。

TL;DR基本上,我如何使用PHP或JS或两者以动态创建的具有重复字段集的形式生成类似foo[0]['name']的东西,而不是foo['name'][0]?

我会这样处理它:

    if(isset($_POST['name'])) {
            // $i is how you are going to assign your keys
            $i = 0;
            // Loop through post array
            foreach($_POST['name'] as $key => $value) {
                    // Assign your new array the key values of each post array
                    $_rows[$i]['name']  =   $_POST['name'][$key];
                    $_rows[$i]['url']   =   $_POST['url'][$key];
                    $_rows[$i]['color'] =   $_POST['color'][$key];
                    $i++;
                }
        /*   Do some quick prints to see before and after
             echo '<pre>';
             print_r($_POST);
             print_r($_rows);
             echo '</pre>';
        */
        }

好的,所以我在我正在开发的插件中做了类似的事情,这就是我提出的解决方案。我修改了我的代码,试图与您想要实现的目标相匹配。

现在,基于您上面的代码,我假设您有一个名为calendars的post-meta。因此,从那里我们需要修改您的代码一点:

// Get our saved post meta if it exists
$calendars  = get_post_meta( $post_id, '_calendars', true );
// Loop through all of our calendars and display the inputs    
if ( $calendars ) { 
    foreach ( $calendars as $calendar ) { 
    ?>
        <label>Calendar Name
            <input name="name[]" id="name[]" type="text" value="<?php echo $calendar['name']; ?>">
        </label>
        <label>Public URL
            <input name="url[]" id="url[]" type="text" value="<?php echo $calendar['url']; ?>">
        </label>
        <label>Color
            <input name="color[]" id="color[]" type="text" value="<?php echo $calendar['color']; ?>">
        </label>    
    <?php
    }
}

在您的保存功能中包括以下代码:

$old_calendar = get_post_meta( $post_id, '_calendars', true );
$new_calendar = array();
$name   = $_POST['name'];
$url    = $_POST['url'];
$color  = $_POST['color'];
// Just to get the number of calendars we have
$count = count( $name );
for ( $i = 0; $i < $count; $i++ ) {
    $new_calendar[$i]['name'] = $name[$i]; // Need sanitation if desired
    $new_calendar[$i]['url'] = $url[$i]; // Need sanitation if desired
    $new_calendar[$i]['color'] = $color[$i]; // Need sanitation if desired 
}
if ( !empty( $new_calendar ) && $new_calendar != $old_calendar )
    update_post_meta( $post_id, '_calendars', $new_calendar );
elseif ( empty($new_calendar) && $old_calendar )
    delete_post_meta( $post_id, '_calendars', $old_calendar );