【问题标题】:Wordpress: Repeatable Fields - Store multiple selectionsWordpress:可重复字段 - 存储多个选择
【发布时间】:2014-09-09 11:49:41
【问题描述】:

编辑:

我不明白如何将多个选择存储在数组中, 只保存第一个值。

img 链接 -> http://oi62.tinypic.com/20p58cl.jpg

有人可以帮我吗?

@maronl 谢谢你的帮助,但是没有用。

请看一下这段代码。 最好的问候。

<?php
/*
Plugin Name: EXAMPLE repeatable-fields

*/


add_action('admin_init', 'add_meta_boxes', 1);
function add_meta_boxes() {
    add_meta_box( 'repeatable-fields', 'EXAMPLE', 'repeatable_meta_box_display', 'post', 'normal', 'high');
}

function repeatable_meta_box_display() {
    global $post;

    $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
         $item_array = array('A','B','C','D');


    wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' );
?>
    <script type="text/javascript">
jQuery(document).ready(function($) {
    $('.metabox_submit').click(function(e) {
        e.preventDefault();
        $('#publish').click();
    });
    $('#add-row').on('click', function() {
        var row = $('.empty-row.screen-reader-text').clone(true);
        row.removeClass('empty-row screen-reader-text');
        row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
        return false;
    });
    $('.remove-row').on('click', function() {
        $(this).parents('tr').remove();
        return false;
    });

    $('#repeatable-fieldset-one tbody').sortable({
        opacity: 0.6,
        revert: true,
        cursor: 'move',
        handle: '.sort'
    });
});
    </script>

    <table id="repeatable-fieldset-one" width="100%">
    <thead>
        <tr>
            <th width="2%"></th>
            <th width="60%">Name</th>
            <th width="60%">Surname</th>
            <th width="10%">Item</th>
            <th width="2%"></th>
        </tr>
    </thead>
    <tbody>
    <?php

    if ( $repeatable_fields ) :

        foreach ( $repeatable_fields as $field ) {
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>

        <td><input type="text" class="widefat" name="surname[]" value="<?php if ($field['surname'] != '') echo esc_attr( $field['surname'] ); else echo ''; ?>" />

        </td>


        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>

        </td>


        <td><a class="sort">|||</a></td>

    </tr>
    <?php
        }
    else :
        // show a blank one
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>


        <td><input type="text" class="widefat" name="surname[]" value="" /></td>

        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>

        </td>
<td><a class="sort">|||</a></td>

    </tr>
    <?php endif; ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>


        <td><input type="text" class="widefat" name="surname[]" value="" /> </td>

        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>

        </td>

<td><a class="sort">|||</a></td>

    </tr>
    </tbody>
    </table>

    <p><a id="add-row" class="button" href="#">Add another</a>
    <input type="submit" class="metabox_submit" value="Save" />
    </p>

    <?php
}

add_action('save_post', 'repeatable_meta_box_save');
function repeatable_meta_box_save($post_id) {
    if ( ! isset( $_POST['repeatable_meta_box_nonce'] ) ||
        ! wp_verify_nonce( $_POST['repeatable_meta_box_nonce'], 'repeatable_meta_box_nonce' ) )
        return;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    if (!current_user_can('edit_post', $post_id))
        return;

    $old = get_post_meta($post_id, 'repeatable_fields', true);
    $new = array();


    $names = $_POST['name'];
    $surnames = $_POST['surname'];
    $items = $_POST['item'];

    $count = count( $names );

    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) :

            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
            $new[$i]['surname'] = stripslashes( $surnames[$i] );
            $new[$i]['item'] = stripslashes( strip_tags( $items[$i] ) );



        endif;
    }

    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'repeatable_fields', $new );
    elseif ( empty($new) && $old )
        delete_post_meta( $post_id, 'repeatable_fields', $old ); }
?>

【问题讨论】:

    标签: php arrays wordpress multidimensional-array jquery-ui-sortable


    【解决方案1】:

    我认为您应该为输入数据添加索引。我制作了一个示例 php 页面,其中包含两个包含相同输入类型的块

    <html>
    
    <head></head>
    
    <body>
    
    <h1>POST VALUES</h1>
    <pre><?php print_r($_POST); ?></pre>
    
    <h1>SAMPLE FORM</h1>
    <form method="POST">
    <h2>first block</h2>
    first name 1<input name="first_name[1]" />
    <br />last name 1<input name="last_name[1]" />
    <br /><select name="item[1][]" multiple>
    <option value="1">primo</option>
    <option value="2">secondo</option>
    </select>
    
    <h2>second block</h2>
    first name 2<input name="first_name[2]" />
    <br />last name 2<input name="last_name[2]" />
    <br /><select name="item[2][]" multiple>
    <option value="1">primo</option>
    <option value="2">secondo</option>
    </select>
    
    <br /><input type="submit" value="submit">
    </form>
    
    </body>
    </html>
    

    以这种方式,当您提交您在 $_POST 中的数据时

    Array
    (
        [first_name] => Array
            (
                [1] => name 1
                [2] => name 2
            )
    
        [last_name] => Array
            (
                [1] => last name 1
                [2] => last name 2
            )
    
        [item] => Array
            (
                [1] => Array
                    (
                        [0] => 1
                        [1] => 2
                    )
    
                [2] => Array
                    (
                        [0] => 2
                        [1] => 3
                    )
    
            )
    
    )
    

    当你打印你的盒子时,应该很简单地为输入数据添加一个索引。像

    $index = 0;
    foreach ($informations as $field): 
      $index++;
      echo '<input name="name['.$index.'][]" value="'. esc_attr($field['name']) .'" />';
      echo '<input name="surname['.$index.'][]" value="'. esc_attr($field['surname']) .'" />';
      ....
    

    希望这能有所帮助!

    【讨论】:

    • 您好,maroni,感谢您的帮助,但没有成功。查看完整代码。
    • 你好。在您的代码中,我看不到我建议的索引。输入名称仍然是 name[], surname[], item[] 并且它们应该是 name[1], surname[1], item[1][] 为第一人称。对于第二个名字[2]、姓[2]、项目[2][]。通过这种方式,您可以为每个人设置不同的项目,而不是合并到一个唯一的数组中。那么在添加新人时,您还必须在 jQuery 代码中管理该索引
    猜你喜欢
    • 2020-07-27
    • 2011-02-13
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多