Show label of Select, Chekbox list
- This topic has 16 replies, 3 voices, and was last updated 1 year, 2 months ago by
Mario Kempter.
-
AuthorPosts
-
October 27, 2020 at 6:31 AM #22588
Trang Nguyen
ParticipantHi, 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.
October 27, 2020 at 10:25 AM #22593Long Nguyen
ModeratorHi,
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/15692487After working with PHP, you can use the proxy
mb.
to apply the code in View.October 27, 2020 at 6:28 PM #22604Trang Nguyen
ParticipantHi 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!
October 27, 2020 at 10:47 PM #22609Long Nguyen
ModeratorHi,
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>";
October 28, 2020 at 9:36 PM #22624Long Nguyen
ModeratorHi,
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/.
October 28, 2020 at 11:23 PM #22627Trang Nguyen
ParticipantHi 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!!
October 29, 2020 at 9:56 AM #22629Long Nguyen
ModeratorHi,
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>"; }
October 30, 2020 at 8:47 PM #22663Trang Nguyen
ParticipantWow! Many thanks, Long. You saved my time.
Everything is perfect now.
Thank you again!November 20, 2020 at 6:55 PM #22952Trang Nguyen
ParticipantHi 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!
November 20, 2020 at 10:59 PM #22955Long Nguyen
ModeratorHi,
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>";
November 24, 2020 at 5:09 PM #23045Trang Nguyen
ParticipantWonderful! It works like a charm.
Many thanks, Long!November 25, 2020 at 4:58 AM #23052Trang Nguyen
ParticipantHi 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!
November 25, 2020 at 9:50 AM #23053Long Nguyen
ModeratorHi,
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.
November 25, 2020 at 5:21 PM #23059Trang Nguyen
ParticipantHi 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.November 26, 2020 at 9:36 AM #23071Long Nguyen
ModeratorHi,
Because only the field
group
has the keyfields
in the settings array. So it will display the warning notice likeInvalid 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
-
AuthorPosts
- You must be logged in to reply to this topic.