从SELECT下拉列表中获取具有变量值的POST数据

时间:2017-12-19 00:00:00

标签: php dropdown postdata

我需要创建与worpress中的数据库表查询中的值一样多的下拉列表。我无法从foreach下拉列表中将数据发布到usermeta表。这是代码:

$custom_posts_type = $wpdb->get_results("SELECT label,id FROM $table_name");
      foreach ($custom_posts_type as $custom_post_type) {
           $custom_post_type_name = $custom_post_type->label;
           $custom_post_type_id = $custom_post_type->id;
           ?>
           <p>   
           <label for="dropdown">Permisos de usuario para <?php echo $custom_post_type_name;?>: </label>
           <?php                                               
           $user_custom_post = 'user_custom_' . $custom_post_type_name;
           $selected = get_the_author_meta( $user_custom_post, $user->ID ); 
           $post_type_object = get_post_type_object($custom_post_type_name);                        
           $label = $post_type_object->label;
           $posts = get_posts(array('post_type'=> $custom_post_type_name, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));                        
           echo '<select name="' . $user_custom_post . '" id="' . $user_custom_post . '">';                                               
            ?>
            <option value="all" <?php selected( $user_custom_post[0], "all" ); ?>>All <?php echo $label;?></option>
            <?php 
            foreach ($posts as $post) {
                echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
             }?>            
           <?php
           echo '</select>';
           ?>                        
           </p>
          <?php
          update_user_meta( $user->ID, $user_custom_post, $_POST[$user_custom_post] );                        
 } //endforeach

感谢您的帮助。 THKS

1 个答案:

答案 0 :(得分:0)

可能......我创造了两个没有foreach的功能。并且,如果我不使用变量,它可以工作:

 add_action( 'show_user_profile', 'pglr_user_pages' );
    add_action( 'edit_user_profile', 'pglr_user_pages' );
    add_action( 'user_register', 'pglr_user_pages' );

    function pglr_user_pages( $user ) { 
            $post_type = "page"; ?>
            <p>   
                <label for="dropdown">Asignar <?php echo $post_type;?>: </label>
                <?php 
                $selected = get_the_author_meta( 'user_custom_page', $user->ID ); 
                $post_type_object = get_post_type_object($post_type);
                $label = $post_type_object->label;
                $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
                echo '<select name="user_custom_page_value" id="user_custom_page_value">';            
                ?>                    
                <option value="all" <?php selected( '', "all" ); ?>>All <?php echo $label;?></option>
                <?php
                foreach ($posts as $post) {
                    echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
                }?>            
                <?php
                echo '</select>';                
                ?>                      
            </p>       
        <?php     
        } 


    add_action( 'personal_options_update', 'save_pglr_user_pages' );
    add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );

    function save_pglr_user_pages( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
    //save dropdown
    update_usermeta( $user_id, 'user_custom_page', $_POST['user_custom_page_value'] );
    }

但是,此代码不起作用:

    add_action( 'show_user_profile', 'pglr_user_pages' );
    add_action( 'edit_user_profile', 'pglr_user_pages' );
    add_action( 'user_register', 'pglr_user_pages' );

    function pglr_user_pages( $user ) { 
           $post_type = "page"; ?>
            <p>   
                <label for="dropdown">Asignar <?php echo $post_type;?>: </label>
                <?php 
                **global $user_custom_post;**
                **$user_custom_post = 'user_custom_' . $post_type;**                                      
                $selected = get_the_author_meta( **$user_custom_post**, $user->ID ); 
                $post_type_object = get_post_type_object($post_type);
                $label = $post_type_object->label;
                $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
                echo '<select name="user_custom_page_value" id="user_custom_page_value">';            
                ?>
                <option value="all" <?php selected( '', "all" ); ?>>All <?php echo $label;?></option>
                <?php
                foreach ($posts as $post) {
                    echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
                }?>            
                <?php
                echo '</select>';                
                ?>                      
            </p>       
        <?php     
        } 

    add_action( 'personal_options_update', 'save_pglr_user_pages' );
    add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );

    function save_pglr_user_pages( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
    //save dropdown

    update_usermeta( $user_id, **$user_custom_post,** $_POST['user_custom_page_value'] );
    }

问题可能是使用变量...