black friday sale

Custom Fields
Re-Envisioned
Support MB Views Show label of Select, Chekbox list

  • Creator
    Topic
  • #22588
    Resolved Trang Nguyen
    Participant

    Hi, I use this code in Views to show the data of Select, Checkbox list…:

    {% for item in clone.mb_day %}
        {{ item.label }}
    {% endfor %}

    How could I add “,” after all the items except the last one that comes with “:”?
    EX: Now I have only MondayTuesdayFriday
    I would like to have “Monday, Tuesday, Friday:”

    Another question: With this PHP code to show data of Select, Checkbox list…:

    $values = rwmb_meta( $'mb_day' );
    foreach ( $values as $clone ) {
        foreach ( $clone as $value ) {
            echo $value;
        }
    }

    How to show the label of each item instead of raw data in the database (Monday, Tuesday instead of mo, tu…)? Also to make them come with “,” and “:” as the first question.

    Thank you in advance.

Viewing 10 replies - 1 through 10 (of 15 total)
  • Author
    Replies
  • #22593
    Long Nguyen
    Moderator

    Hi,

    Please use the helper function rwmb_get_field_settings() to get the field’s settings: field’s name, field’s options (value, label) …

    And follow these guidelines to know how to add a comma for each item but the last one in the array
    https://www.sitepoint.com/community/t/comma-after-each-item-in-array-except-last/18234
    https://stackoverflow.com/questions/15692464/php-foreach-separating-each-loop-with-comma-except-the-last-element/15692487

    After working with PHP, you can use the proxy mb. to apply the code in View.

    #22604
    Trang Nguyen
    Participant

    Hi Long,
    Many thanks for your links. I could solve the comma issue in no time 🙂

    But I couldn’t solve the label issue. Honestly, I know nothing about PHP, just copy and paste.
    I tried this code but it didn’t help:

    $field = rwmb_get_field_settings( 'mb_day');
    foreach ( $field as $a ) {
       echo $a;
    }

    I know I have to put something before the loop but I have no idea what.
    Could you please help me to modify it to get the labels!

    It would be great if we have an example on the rwmb_get_field_settings page.

    Thank you!

    #22609
    Long Nguyen
    Moderator

    Hi,

    You can use this code to print the label of the selected option.

    $select = rwmb_meta( 'select_id' );
    $select_settings = rwmb_get_field_settings( 'select_id' );
    foreach ( $select_settings as $key => $value ) {
        if ( $key == 'options' ) {
            echo $value[$select];
        }
    }

    Use this code to print out the array elements when getting the field settings.

    echo "<pre>";
    print_r( $select_settings );
    echo "</pre>";
    #22624
    Long Nguyen
    Moderator

    Hi,

    In short, we can use the function rwmb_the_value() to show the label of the selected option. See more in this documentation https://docs.metabox.io/rwmb-the-value/.

    #22627
    Trang Nguyen
    Participant

    Hi Long,
    Many thanks for your help!

    I tried rwmb_the_value() and it worked great. Easy and fast. (Thanks again)

    But for the fields with multiple choices, rwmb_the_value() always renders with HTML (which I don’t want). So I tried the code rwmb_get_field_settings you gave me above:

    $select = rwmb_meta( 'select_id' );
    $select_settings = rwmb_get_field_settings( 'select_id' );
    foreach ( $select_settings as $key => $value ) {
        if ( $key == 'options' ) {
            echo $value[$select];
        }
    }

    It works well with single choice select fields but seems like it doesn’t work with multiple choice select fields.
    I used the same code, only changed the field setting to multiple choices and it stopped working, with no “echo” info on the front-end.
    If I change the field setting back to single choice, it works well as before.

    So could you please give me a solution for a field with multiple choices!

    Thank you!!

    #22629
    Long Nguyen
    Moderator

    Hi,

    The value returns when getting multiple choices is an array so we need to use two loops:

    $multi_select = rwmb_meta( 'select_id' );
    $select_settings = rwmb_get_field_settings( 'select_id' );
    foreach ( $multi_select as $select_value ) {
        foreach ( $select_settings as $setting_key => $setting_value ) {
            if ( $setting_key == 'options' ) {
                echo $setting_value[$select_value];
            }
        }
    }

    and the cloneable multiple choices returns an array of arrays:

    $clone_multi_select = rwmb_meta( 'select_id' );
    $select_settings = rwmb_get_field_settings( 'select_id' );
    foreach ( $clone_multi_select as $clone_key => $clone ) {
        echo "Clone {$clone_key} :";
        foreach ( $clone as $select_value ) {       
            foreach ( $select_settings as $setting_key => $setting_value ) {
                if ( $setting_key == 'options' ) {
                    echo $setting_value[$select_value];
                }
            }   
        }
        echo "<br>";
    }
    #22663
    Trang Nguyen
    Participant

    Wow! Many thanks, Long. You saved my time.
    Everything is perfect now.
    Thank you again!

    #22952
    Trang Nguyen
    Participant

    Hi Long,
    Sorry for opening this topic again.

    I have other select fields that are in a group field in a custom table. I edited your code to work in a custom table and a group field but I got some error warning and it didn’t work

    $group = rwmb_meta( 'group', ['storage_type' => 'custom_table', 'table' => 'custom'] );
    $multi_select = isset( $group['language'] ) ? $group['language'] : '';
    $select_settings = rwmb_get_field_settings( $group['language'] );
    foreach ( $multi_select as $select_value ) {
        foreach ( $select_settings as $setting_key => $setting_value ) {
            if ( $setting_key == 'options' ) {
                echo $setting_value[$select_value];
            }
        }
    }

    I think I make something wrong on the line
    $select_settings = rwmb_get_field_settings( $group[‘language’] );

    But I really don’t know what wrong. Please help!

    Thank you!

    #22955
    Long Nguyen
    Moderator

    Hi,

    Yes, you have to pass the group ID to the function rwmb_get_field_settings(). It would be:

    rwmb_get_field_settings( 'group' )

    To print the selected label of a select field (multi select) in a group, please use this code:

    $group = rwmb_meta( 'group', ['storage_type' => 'custom_table', 'table' => 'custom'] );
    $multi_select = isset( $group['language'] ) ? $group['language'] : '';
    $group_settings = rwmb_get_field_settings( 'group' );
    
    foreach ( $multi_select as $select_value ) {
        foreach ( $group_settings['fields'] as $field_settings ) {
            foreach ( $field_settings as $setting_key => $setting_value ) {
                if ( $setting_key == 'options' ) {
                    echo $setting_value[$select_value];
                }
            }
        }
        
    }

    Hint: You can use the code below to print out the structure of group settings then you will see what you need to create the code.

    echo "<pre>";
    print_r( $group_settings );
    echo "</pre>";
    #23045
    Trang Nguyen
    Participant

    Wonderful! It works like a charm.
    Many thanks, Long!

Viewing 10 replies - 1 through 10 (of 15 total)
  • You must be logged in to reply to this topic.