BuddyDev

Search

[Resolved] Count results

  • Participant
    Level: Initiated
    Posts: 1
    Itsa on #50515

    I am organizing a meetup of all members with dogs. I have done a short loop listing all members that entered any value in the xprofile field “Dogs”, and of those members, listing the value of xprofile field “Meals”.

    <?php if ( bp_has_members( my_custom_ids( 'Dogs' ) . '&type=alphabetical' ) ) : ?>
    	<?php bp_nouveau_pagination( 'top' ); ?>
    	<table id="members-list" class="<?php bp_nouveau_loop_classes(); ?>">
                <thead>
                    <tr>
                    <th>Name</th>
                    <th>Meal Preference</th>
                    </tr>
                </thead>
    	<?php while ( bp_members() ) : bp_the_member(); ?>
            <?php $s1_meals = xprofile_get_field_data( 'Meals' , bp_get_member_user_id() ); ?>
    	    <tr <?php bp_member_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_member_user_id(); ?>" data-bp-item-component="members">
                    <td class="list-title member-name">
                        <?php bp_member_name(); ?>
                        <?php $member_id = bp_get_member_user_id(); ?>
                    </td>
                    <td class="<?php echo $s1_meals; ?>">
                        <?php echo $s1_meals; ?>
                    </td>
                </tr>
            <?php endwhile; ?>
        </table>
    <?php bp_nouveau_pagination( 'bottom' ); ?>
    <?php else : bp_nouveau_user_feedback( 'members-loop-none' ); endif; ?>

    It gives me a table with member names and their meal preference (omnivore, vegetarian, vegan). How would I then do a final count of omnivores, vegetarians, and vegans? I’d like a list that says:

    Omnivore: 25
    Vegetarian: 12
    Vegan: 8

    My attempts at doing a count always return a total count of people who have dogs (45), not the subset of those people who are omnivores (25), for example. Thank you for your help!

  • Keymaster
    (BuddyDev Team)
    Posts: 24298
    Brajesh Singh on #50518

    Hi,
    Welcome to BuddyDev.

    You need to count the unique users by meal and print stats.
    here is slightly modified code that will do it for you.

    Please feel free to adapat as you please.

    
    <?php
    $meal_stats = array();
    if ( bp_has_members( my_custom_ids( 'Dogs' ) . '&type=alphabetical' ) ) : ?>
    	<?php bp_nouveau_pagination( 'top' ); ?>
    	<table id="members-list" class="<?php bp_nouveau_loop_classes(); ?>">
    		<thead>
    		<tr>
    			<th>Name</th>
    			<th>Meal Preference</th>
    		</tr>
    		</thead>
    		<?php while ( bp_members() ) : bp_the_member(); ?>
    			<?php $s1_meals = xprofile_get_field_data( 'Meals', bp_get_member_user_id() );
    			// initialize.
    			if ( ! isset( $meal_stats[ $s1_meals ] ) ) {
    				$meal_stats[ $s1_meals ] = array();
    			}
    
    			$meal_stats[ $s1_meals ][] = bp_get_member_user_id();
    			?>
    
    			<tr <?php bp_member_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_member_user_id(); ?>" data-bp-item-component="members">
    				<td class="list-title member-name">
    					<?php bp_member_name(); ?>
    					<?php $member_id = bp_get_member_user_id(); ?>
    				</td>
    				<td class="<?php echo $s1_meals; ?>">
    					<?php echo $s1_meals; ?>
    				</td>
    			</tr>
    		<?php endwhile; ?>
    	</table>
    	<?php
    	// print stats.
    	foreach ( $meal_stats as $meal => $meal_stat ) {
    		if ( empty( $meal_stat ) ) {
    			continue;
    		}
                    echo "<br/>";
    		echo "Meal: " . $meal . " : ";
    		echo count( array_unique( $meal_stat ) );
    		echo "<br/>";
    	}
    	?>
    	<?php bp_nouveau_pagination( 'bottom' ); ?>
    <?php else : bp_nouveau_user_feedback( 'members-loop-none' ); endif; ?>
    
    

    Hope it helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 1
    Itsa on #50572

    That works perfectly! Thank you for helping me out.

  • Keymaster
    (BuddyDev Team)
    Posts: 24298
    Brajesh Singh on #50589

    You are welcome.
    I am glad it worked!

    Regards
    Brajesh

The topic ‘ [Resolved] Count results’ is closed to new replies.

This topic is: resolved