BuddyDev

Search

[Resolved] Fetch and condition for checkboxes data values

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

    Hello great team:)

    I have a checkbox field called ‘Roles’ with 3 checkboxes that have titles ‘Music’ , ‘Movies’ , ‘Food’.

    I would like to create some if statement according to what the user checked.
    I saw from https://buddydev.com/fetching-and-showing-only-specific-fields-from-buddypress-xprofile-data/ that i can use $data = bp_get_member_profile_data( array( 'field'=>'CheckBox Or Select Field Name','user_id' => $user_id ) );echo $data; to fetch the data, but this returns all data in an array.

    Could you please help me create an if statement like if user with $user_id checked the checkbox 'Music' from checkbox field called 'Roles' because i can’t figure it out.

    As i saw here https://buddypress.org/support/topic/buddypress-checkbox-field-data/ the data in the database are in a form of a serialized array.

    Thank you very much for your time and help

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #43967

    Hello Lefteris,

    Thank you for posting here. Please try the function with the name “xprofile_get_field_data” use the third argument as ‘comma’. Please try this and let me know if it helps or not.

    Regards
    Ravi

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

    Hello Ravi,

    Thank you so much for your feedback. You have been a savier for me 🙂

    I have tried to do:

    $data = xprofile_get_field_data( 12, $user_id = 23, 'comma' );
    echo $data;

    but it doesn’t give me any result . Maybe i am doing something wrong?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #43988

    Hello Lefteris,

    You are calling function incorrectly. Please call like following:

    
    $data = xprofile_get_field_data( 12, 23, 'comma' )
    
    

    Please give it a try

    Regards
    Ravi

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

    I just tried, it didn’t gave me any result either . I’ve even tried to set ‘array’ instead of ‘comma’.

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

    A text field is giving me results normal.

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

    I am sorry for the confusion Ravi, yes it gives me a correct result Music , Movies , Food . But how i can create an if statement to see if music is ticked, or if music+food is ticked, or if none is ticked?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #44003

    Hello Lefteris,

    Please try the following way:

    
    $data = xprofile_get_field_data( 12, 23 );
    
    if ( ! $data ) {
    	// No item is selected.
    }
    
    if( in_array( 'Music', $data ) ) {
    	// Music is selected.
    }
    
    // Same goes with other one.
    
    

    Regards
    Ravi

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

    Hello Ravi,

    Thank you for your help. I have tried this and it work fine. However when the user ticks 2 out of 3 checkboxes, or all checkboxes. it doesn’t work. Any ideas?

    Regards Lefteris

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #44006

    Hello Lefteris,

    Please share your complete code of yours so that I can check.

    Regards
    Ravi

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

This topic is: resolved