Hello Ravi, I did something like this. I didn’t use
elseif
.`$data = xprofile_get_field_data( 12, 23, ‘comma’);
if ( ! $data ) { // No item is selected.}
if ( in_array( ‘Music’, $data ) ) { // Music is selected.}
if ( in_array( ‘Music’, ‘Movies’, $data ) ) { // Music and movie is selected.}
if ( in_array( ‘Music’, ‘Movies’, ‘Food’, $data ) ) { // All is selected.}`
Regards Lefteris
Hello Lefteris,
Please remove ‘comma’ as an argument as we need field output as array for below conditions. You are also calling in_array incorrectly.
For multi value check use:
// Multivalue and make sure $data is an array. if ( ! array_diff( array( 'Music', 'Movie' ), $data ) ) { // Both values are present in $data array. }
Please check
Regards
Ravi- This reply was modified 2 years, 7 months ago by Ravi.
Hello Ravi,
Now it works, but there is one problem. The output is giving me results for each if that is
true
.So i have tried to set
elseif
because i would like only one output according to users checks. Withelseif
i get correct results only for this type of statements.if ( in_array( โMusicโ, $data ) ) { // Music is selected.}
For
array_diff
statements i get wrong results.Regards Lefteris
Hello Ravi, thank you for your help ๐ This is what happens with
elseif
statements:$data = xprofile_get_field_data( 12, 23, โcommaโ); if ( ! $data ) { echo "No item is selected";} elseif ( in_array( โMusicโ, $data ) ) { echo "Music is selected";} -> outputs: Music is selected elseif ( ! array_diff( array( 'Music', 'Movies' ), $data ) ) { echo "Music and Movies are selected";} -> outputs: Music is selected (should output: Music and Movies are selected ) elseif ( ! array_diff( array( 'Music', 'Movies', 'Food' ), $data ) ) { echo "All are selected";}
-> outputs: Movies is selected (should output: All are selected )
I will need to have only one output result according to users checks.
With the previous code with only
if
statements , i was getting as an output for example:if ( ! array_diff( array( 'Music', 'Movies', 'Food' ), $data ) ) { echo "All are selected";}
-> outputs: Music Movies Food Music is selected Food is selected Movies is selected Music and Movies are selected Music and Food are selected…. (because it was outputting all the
if's
. Should just output: All are selected )I hope this helps
Regards Lefteris
Hello Lefteris,
Please try the following code:
$data = xprofile_get_field_data( 12, 23 ); $count = $data ? count( $data ) : 0; $message = ''; if ( empty( $data ) ) { $message = __( 'No item is selected' ); } elseif ( 2 < $count ) { $message = __( 'All are selected' ); } elseif ( 1 < $count ) { $message = sprintf( '%s are selected', join( ' and ', $data ) ); } else { $message = sprintf( '%s is selected', join( '', $data ) ); } echo esc_html( $message );
Please check
Note not recommended as increasing complexity.
Recommended
$data = xprofile_get_field_data( 12, 23 ); $message = ''; if ( empty( $data ) ) { $message = __( 'No item is selected' ); } elseif ( count( $data ) > 1 ) { $message = sprintf( '%s are selected', join( ' and ', $data ) ); } else { $message = sprintf( '%s is selected', join( '', $data ) ); }
Regards
RaviHello Ravi,
Let me explain i bit better what i actually need to do ๐ On BP registration page i have a checkbox that users can check if they wish (optional field and not required).
Along with his checkbox preference i am sending a welcome email to the user.
Something like
function send_welcome_email_to_new_user($user_id) { $data = xprofile_get_field_data( 12, $user_id ); $message = sprintf( 'Hello' ) . "<br>"; if ( statements ) ) { $message .= sprintf( 'Music is selected' ) ."<br><br>"; } elseif ( statements ) ) { $message .= sprintf( 'Food is selected' ) ."<br><br>"; }elseif ( statements ) ) { $message .= sprintf( 'Food and music are selected' ) ."<br><br>"; } $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($to, $subject, $message,$headers); } add_action( 'bp_core_activated_user', 'send_welcome_email_to_new_user');
So actually i need the structure that we have been speaking about before ๐
Regards Lefteris
Hello Lefteris,
Try the following code:
$data = xprofile_get_field_data( 12, 23 ); $message = ''; if ( empty( $data ) ) { $message = __( 'No item is selected' ); } elseif ( count( $data ) > 1 ) { $last = array_pop($data); $selected_options = join(', ', $data ) . __( ' and ' ) . $last; $message = sprintf( '%s are selected', $selected_options ); } else { $message = sprintf( '%s is selected', join( '', $data ) ); } echo esc_html( $message );
Please let me know if it helps or not.
Regards
RaviHello Ravi,
Thank you so much for your help ๐
I am sorry if i confused you. The
$message
information is not related with the$data
. Its just an introduction message text, and i don’t really need data from the$data
. The text ‘Food is selected’ was just an example. I just need to pass theif statements
for each users choice so that i can send with wp_mail the appropriate text accordingly.Regards Lefteris
Hello Lefteris,
Please let me know if the above-mentioned code is giving you the following result or not.
My code will make the output like
// If all three options are selected.
Music, Food and Movie are selected.// If two options are selected.
Music and Movie are selected.// If only one option is selected.
// Music is selectedRegards
Ravi
The topic ‘ [Resolved] Fetch and condition for checkboxes data values’ is closed to new replies.