BuddyDev

Search

[Resolved] Fetch and condition for checkboxes data values

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44007

    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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #44008

    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 ago by Ravi.
  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44012

    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. With elseif 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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #44013

    Hello Lefteris,

    Thank you for the acknowledgement. Please share the example field output you are looking for. You can share the screenshot as well which make me more clear on your requirement so that I can help.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44016

    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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #44019

    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
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44031

    Hello 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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #44034

    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
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44042

    Hello 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 the if statements for each users choice so that i can send with wp_mail the appropriate text accordingly.

    Regards Lefteris

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #44063

    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 selected

    Regards
    Ravi

The topic ‘ [Resolved] Fetch and condition for checkboxes data values’ is closed to new replies.

This topic is: resolved