Skip to:
Content
Pages
Categories
Search
Top
Bottom

Profile Fields Questions

  • @gian-ava

    Participant

    Question #1:

    I am using the following technique to get single profile values for members pages.

    This in “bp-custom.php”:

    // Get single value from profile fields

    function custom_xprofile( $field ) {

    echo bp_custom_get_member_list_xprofile_data( $field );

    }

    function bp_custom_get_member_list_xprofile_data( $field ) {

    global $site_members_template;

    return xprofile_get_field_data( $field, $site_members_template->member->id );

    }

    And this is the snippet for an “About me” widget:

    <?php if ( bp_has_profile('profile_group_id=7') ) : ?>

    <h3>About me:</h3>

    <?php custom_xprofile('About me') ?>

    <?php endif; ?>

    I get the value right, but the problem is that on members profile that DIDN’T fill in the “About me” field, the heading (h3) “About me” still shows. Not the value, just the heading.

    Is there something wrong in my conditional statement?

    Here’s my structure for the sidebar (I want some widgets on all pages and profile fields widgets on members pages):

    <?php if (!bp_is_member()): //if not profile ?>

    // widgets

    <?php else: ?>

    <?php if ( bp_has_profile('profile_group_id=7') ) : ?>

    <h3>About me:</h3>

    <?php custom_xprofile('About me') ?>

    <?php endif; ?>

    // other widgets

    <?php endif; ?>

Viewing 12 replies - 1 through 12 (of 12 total)
  • @jivany

    Participant

    You’re missing a semi-colon after the custom_xprofile call. That will likely bugger up PHP and might be the source of your issue.

    @djpaul

    Keymaster

    Nah, you can leave semi-colons off the end of lines when it’s used like that. Good spot though. It’s because your function just retrieves the value from the database, and if it’s blank, then it’s blank. Problem is, you are printing the <h3> regardless.

    @gian-ava

    Participant

    Thanks jivany. I corrected that.

    However, that is not the source of the porblem.

    I am trying to achieve different things.

    One is to say, if that field is not filled in, don’t output this block.

    Something like:

    <?php if (custom_xprofile('About me') ) : ?>

    <h3>About me:</h3>

    <?php custom_xprofile('About me'); ?>

    <?php endif; ?>

    But as simple as it is, sometimes I am lost on PHP basics.

    @gian-ava

    Participant

    And another thing I am trying know (Question #2) is if it is possible to use a conditional statement to check if more than 1 group has fileds that has been filled in and return something accordingly:

    <?php if ( bp_has_profile('profile_group_id=9') || bp_has_profile('profile_group_id=7') ) : ?>
    <?php while ( bp_profile_groups() ) : bp_the_profile_group(); if ( bp_profile_group_has_fields() ): //groups loop ?>

    <div id="widget1">

    (stuff from group 9)

    (stuff from group 7)

    </div>

    <?php endif; endwhile; ?>
    <?php endif; ?>

    After experimenting, I couldn’t make it work.

    I need that because in my design I’d like to output a “Personal stuff” widget that has a graphic title and background, and it should display only if some fields from groups 9 and 7 are filles in, otherwise it should’t.

    Is that achievable?

    That is a guru question, isn’t it?

    @jivany

    Participant

    I think you need to look at what @DJPaul said. If your function always returns true then you’re always going to output something, even if that something is blank.

    function custom_xprofile( $field ) {
    echo bp_custom_get_member_list_xprofile_data( $field );
    }

    That echo will always return something. You need to dig into bp_custom_get_member_list_xprofile_data and see if there’s a way to determine if $field exists.

    @gian-ava

    Participant

    It’s really just tjis that I need for problem #1:

    <?php if (custom_xprofile('About me') ) : ?>

    <h3>About me:</h3>

    <?php custom_xprofile('About me'); ?>

    <?php endif; ?>

    If the About me field is filled, it should display

    <h3>About me:</h3>

    if it is not filled in, it shouldn’t display the h3 heading.

    And with the above snippet, it always displays it. It’s the conditional statement that I cannot get right.

    @jivany

    Participant

    Yes, but your custom_xprofile call just does an “echo”. echo will always return something. Something might be a string or it might be a blank but it will always return something. You can’t test a function return value if you aren’t returning a value. Inside custom_xprofile, you need a “return TRUE” or something like that to be able ot use it in a boolean logic check that the if does.

    @jivany

    Participant

    I took another look at this and I think I have a potential solution.

    You can filter xprofile_get_field_data and make sure that the return value is not a blank or some other non value. The downside of this method is I think you would need to then set the returned string to a known value that indicates it is not populated. The only reason I say this is ideally, you probably want to make bp_custom_get_member_list_xprofile_data return FALSE if the string is blank but I don’t think you can do this within the filter.

    As for your second issue, a really hallf-assed way to do it is with two loops. I’m sure there’s a more elegant way but I’ve never written elegant code. ;)

    That said, I don’t see a way to dump out two groups in one loop but I’m not very familiar with this code.

    @jivany

    Participant

    @gian-ava: For your second question, check out this post https://buddypress.org/forums/topic/faq-how-to-code-snippets-and-solutions#post-13243

    I haven’t tried this but it would suggest you could do something like:

    <?php if ( bp_has_profile('profile_group_id=9') || bp_has_profile('profile_group_id=7') ) : ?>
    <?php while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
    <div id="widget1">
    <?php if (bp_the_profile_group() == "9") : ?>
    // Do whatever you want with group 9
    <?php endif; ?>
    <?php if (bp_the_profile_group() == "7") : ?>
    // Do whatever you want with group 7
    <?php endif; ?>
    </div>
    <? endwhile; ?>
    <?php endif; ?>

    The only part I’m not sure about is if you need the first call to bp_the_profile_group() on the second line (after the start of the while loop).

    @gian-ava

    Participant

    jivany, thanks a lot for taking an interest in this. I appreciate.

    I eventually ended up re-arranging things to have the best with what BP provides (limitations?).

    But I will test the above code and see if I get want I wanted.

    I’ll report later.

    Thanks.

    ;-)

    @blauweogen

    Participant

    For anyone else looking for getting an if statement to work, I got to work. This is from member-loop.
    `

    some text

    Here is the same for the member-header.

    some text

    `

    @bowromir

    Participant
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Profile Fields Questions’ is closed to new replies.
Skip to toolbar