Skip to:
Content
Pages
Categories
Search
Top
Bottom

Performance between loop and all conditional profile fields export

  • Avatar of Shmoo
    Shmoo
    Participant

    @macpresss

    Is it bad coding to export all profile fields by a conditional tag instead of the default profile loop ?

    Something like this..

    
    <?php if ( $data = bp_get_profile_field_data( 'field=Country ' ) ) : ?>
    <div class="profile_fields">
             Country: <span><a href="..../?s=<?php echo $data ?>" title="<?php echo $data ?>">
    <?php echo $data ?></a></span>
    </div>
    <?php endif ?>
    

    This would give me ultimate control about the CSS and layout front-end of each field but how bad is it for performance of the site ?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Hi @macpresss

    What do you mean by ‘export all profile fields by a conditional tag’?

    Doing what you’ve done above is good practice because if Country is an optional field and it has not been completed by the member then nothing will be outputted:

    <?php if ( $data = bp_get_profile_field_data( 'field=Country ' ) ) : ?>
         <!-- do something -->
    <?php endif ?>

    If you’re doing something with a single profile field then there is no need to loop through them all. It’s basically wasted effort and slower performance.

    Avatar of Shmoo
    Shmoo
    Participant

    @macpresss

    I know, but what if I duplicated the code above for all 20 profile fields ?

    - country
    - about
    - gender
    - age

    I would like to know how bad this would be for performance on the site, from my understanding it has something to do with Database Query’s ?

    Trying to make a Tab view of the profile info, on the first tab I would like to show all general stuff and on the second tab I would like to show work info + social media options.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    bp_get_profile_field_data() queries the database once, so every time you use it will result in a database query taking place. You’re right to say lots of database querying impacts performance. Performance will depend on how many queries you’re using, your hardware spec and the amount user activity your site generates.

    where are you planning to use the code?

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    get the profile loop and use array variables

    Avatar of Shmoo
    Shmoo
    Participant

    @macpresss

    In the profile-loop.php ( members/single/profile )

    Instead of this loop.

    
    <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
    	<?php if ( bp_field_has_data() ) : ?>
    	<tr<?php bp_field_css_class(); ?>>
    		<td class="label"><?php bp_the_profile_field_name(); ?></td>
    		<td class="data"><?php bp_the_profile_field_value(); ?></td>
    	</tr>
    	<?php endif; ?>
    <?php do_action( 'bp_profile_field_item' ); ?>
    <?php endwhile; ?>
    
    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    This code is just a start, I did not test to see if field groups will break it but you can get an example of spitting out fields as an object array variable on profile load and then using the variable to get the values instead of requesting each time.

    put this in bp-custom.php

    
    // creates global object array from profile fields
    function bp_profile_fields_array() {
    	global $fields;
    
    	$fields = array();
    
    	if ( bp_has_profile() ) :
    
    		while ( bp_profile_groups() ) : bp_the_profile_group();
    
    			if ( bp_profile_group_has_fields() ) :
    
    				while ( bp_profile_fields() ) : bp_the_profile_field();
    
    					if ( bp_field_has_data() ) :
    
    							$field_name = bp_get_the_profile_field_name();
    							$field_value = bp_get_the_profile_field_value();
    
    							$field = array(
    								'field_name' => $field_name,
    								'field_value' => $field_value,
    							);
    
    							$fields[] =	$field;
    
    					endif;
    
    				endwhile;
    
    			endif;
    
    		endwhile;
    
    	endif;
    
    	return $fields;
    
    }
    add_action( 'bp_before_member_header', 'bp_profile_fields_array' );
    
    // echoes value of field from object array based on param
    function bp_get_single_profile_field( $param ) {
    	global $fields;
    
    	foreach ( $fields as $key => $val ) {
    	   if ( $val['field_name'] === $param ) {
    		   echo $val['field_value'];
    	   }
    	}
    }

    Then in your member templates:

    <?php bp_get_single_profile_field('PROFILE FIELD NAME'); ?>

    Avatar of Shmoo
    Shmoo
    Participant

    @macpresss

    Thanks guys,

    There is a huge difference between the example I found on the web and the option @modemlooper gave.

    My example:
    performance

    @Modemlooper‘s example:
    performance

    Don’t know if it’s really that bad because they both load around the same time but still a lot more queries.

    Avatar of somethingelse
    somethingelse
    Participant

    @somethingelse

    would this also work in members-loop?
    i was to display a specific summary, including a link to the member’s own website (which is theoretically in a profile field if they fill it in) on the main member’s listing page.

    not quite having success with the conditional display.

    using

    <?php if ( $data = bp_get_profile_field_data( 'field=Website' ) ) : ?>
             	<span class="activity"><a href="<?php echo $data ?>" target="_blank">Website</a></span>
    <?php endif ?>

    and it won’t display anything, even if there is data in the Website field…
    but if i remove the if statement and just use
    <span class="activity"><a href="<?php bp_member_profile_data( 'field=Website' ); ?>" target="_blank">Website</a></span>
    i get a nice linked “Website” – except that EVERY one is linked, even if there isn’t an actual URL in the field. Empty profile fields just link it back to the member listing page.

    any suggestions?

    Avatar of somethingelse
    somethingelse
    Participant

    @somethingelse

    ok – so, after much head-beating-on-keyboard…. a simple revision has given me the results desired:

     <?php if ($data=bp_get_member_profile_data( 'field=Website' ) ) : ?>
             	<span class="activity"><a href="<?php bp_member_profile_data( 'field=Website' ); ?>" target="_blank">Website</a></span><?php endif ?>

    see the difference?
    Original attempt didn’t work: <?php if ( $data = bp_get_profile_field_data( 'field=Website' ) ) : ?>

    Revised line that now works:($data=bp_get_member_profile_data( 'field=Website' ) )

    now it works just as i wanted!
    old vs new bp maybe? not sure. i’m a hack… i just keep tryin’ till i get what i need.
    but i tried for HOURS with multiple configurations and variations on the theme with zero success… today with fresh eyes and brain, noticed that one little discrepancy and voila!

    lord.

Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.