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 5 replies - 11 through 15 (of 15 total)
  • Author
    Replies
  • #23052
    Trang Nguyen
    Participant

    Hi Long,
    It’s me again and sorry for disturbing you too much.
    I would like to ask could I use this way for a multiple select inside 2 levels of groups?

    I have fields like this:

    group1 (cloneable)
        |__group2 (cloneable)
            |__language (multiple select)

    (language is inside group2, group2 is inside group1)

    I used your hint

    echo "<pre>";
    print_r( $group_settings );
    echo "</pre>";

    and saw that I have to take out 2 ‘fields’ of groups settings.

    So I tried this code

    $group1 = rwmb_meta( 'group1', ['storage_type' => 'custom_table', 'table' => 'custom'] );
    $group1_settings = rwmb_get_field_settings( 'group1' );
    
    foreach ( $group1 as $group1_value ) {
        $group2 = $group1_value['group2'];
        
        foreach ( $group2 as $group2_value ) {
            $multi_select = $group2_value['language'];
            foreach ( $multi_select as $select_value ) {
                foreach ( $group1_settings['fields'] as $group1_field_settings ) {
                    foreach ( $group1_field_settings['fields'] as $field_settings ) {
                        foreach ( $field_settings as $setting_key => $setting_value ) {
                            if ( $setting_key == 'options' ) {
                                echo $setting_value[$select_value];
                            }
                        }
                    }
                }
            }
        }
    }

    Unlucky me, I got error on the line
    foreach ( $group1_field_settings[‘fields’] as $field_settings ) {

    I’m so sorry, I’m bad at PHP. Please take a look a help me to solve this issue.

    Many many thanks!

    #23053
    Long Nguyen
    Moderator

    Hi,

    Your code runs smoothly on my local site without any error. If you have an error on any line, print out the variable before using the loop. See https://share.getcloudapp.com/12uKqk4p.

    #23059
    Trang Nguyen
    Participant

    Hi Long, I just double-checked.
    It’s true that the code runs smoothly with that field structure. Many thanks for recording the video.

    But the field structure I’m using has some other fields on the same level as group2. I have something likes this:

    group1 (cloneable)
        |__text
        |__select
        |__group2 (cloneable)
            |__language (multiple select)

    And it leads to error on the line
    foreach ( $group1_field_settings[‘fields’] as $field_settings ) {

    If I remove the text and select fields, everything works well again.
    But if I add any field into the group1, the error will appear.

    I think it is because the group2 has different data order than the text / select field. But I don’t know how to fix it.

    Please help!
    Thank you in advance.

    #23071
    Long Nguyen
    Moderator

    Hi,

    Because only the field group has the key fields in the settings array. So it will display the warning notice like Invalid argument supplied for foreach(). To solve this issue, you should check if the key exists in the array before using the loop.

    foreach ( $group1_settings['fields'] as $group1_field_settings ) {
        if( array_key_exists( 'fields', $group1_field_settings ) ) {
            foreach ( $group1_field_settings['fields'] as $field_settings ) {
                ...
            }
        }
    }

    See more in https://www.php.net/manual/en/function.array-key-exists.php

    #23076
    Trang Nguyen
    Participant

    It works!!!!
    I sincerely thank you and appreciate your patience, Long.
    Have a nice night!

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