Support

Account

Home Forums General Issues Show SELECTED choice from user data.

Helping

Show SELECTED choice from user data.

  • I’ve added a selection list to my members. They can make a selection and it stores the value in their users meta data. I have used the following code to call the selection list and dynamically create the options available..

    BUT, I have kind of forced the SELECTED value in there at the top. This works, but its not the best way to do it. Is there something else I could do?

    <label for="email">Customer Type</label>
    <?php
    $field_key = "field_56aba9ca0d64a";
    $field = get_field_object($field_key, 'user_'.$current_user->ID);
    								
    if( $field ) {
    echo '<select id="acf-' . $field['key'] . '" name="acf[' . $field['key'] . ']" style="width: 100%;">';?>
    
    <!-- forcing the selected choice to show up -->
    <option value="<?php the_author_meta( 'customer_type', $current_user->ID ); ?>" selected="selected" data-i="0"><?php the_author_meta( 'customer_type', $current_user->ID ); ?></option>
    <!-- end -->
    
    <?php foreach( $field['choices'] as $k => $v )
    {											echo '<option value="' . $k . '">' . $v . '</option>';
    }
    echo '</select>';
    }
    ?>

    The reason I don’t like this solution is that it Doubles Up on one of the choices by adding it again to the top of the list:

    Dropdown Selection List Issue

    Thanks for your help!

  • Hi @juiceex

    I believe you can check if the options is selected or not. Something like this:

    foreach( $field['choices'] as $k => $v ){
        $selected = get_the_author_meta( 'customer_type', $current_user->ID );
        if ($k != $selected ){
            echo '<option value="' . $k . '">' . $v . '</option>';
        }
    }

    I hope this helps.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.