Skip to:
Content
Pages
Categories
Search
Top
Bottom

group member names push into array not working


  • Kiciana
    Participant

    @kiciana

    Hi

    I’m trying to get current group members’ names into an array to return them to AJAX function but it seems like the function doesn’t return it.

    function my_action_callback() {
      global $wpdb;
    				
      $names = array();
    
      while ( bp_group_members() ) : bp_group_the_member();
    								
    	array_push($names, bp_group_member_name() );
    								
      endwhile;
      echo json_encode($names);
      die();
    }

    And AJAX:

    jQuery('.ajaxTrigger').live('click',function(){  
    
    	// We can also pass the url value separately from ajaxurl for front end AJAX implementations
    	jQuery.post(ajax_object.ajax_url, function(response) {
    		alert(response[0]);
    	});
    });

    Alert just shows ‘0’

    Thanks in advance for help

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

  • shanebp
    Moderator

    @shanebp

    There are several things to be changed.

    You don’t need this: global $wpdb;

    Change: array_push($names, bp_group_member_name() );

    To: $names[] = bp_get_group_member_name();

    Change the js ajax call to:

    jQuery.post(ajaxurl, function(response) {
        console.log(response); // so you can examine the structure
    });

    Kiciana
    Participant

    @kiciana

    Thank for the reply.

    I tried it but then even the alert doesn’t show when I change the code.

    This is the entire code: [ edited: see below for link to code ]


    shanebp
    Moderator

    @shanebp

    Please use pastebin or gist to share large chunks of code.


    Kiciana
    Participant

    @kiciana

    sure, sorry about that ^^’

    here it is:

    http://pastebin.com/XTNhRUVw


    Kiciana
    Participant

    @kiciana

    Hi

    I figured out that it’s the while loop that’s stopping the php function from responding properly

    While this works just fine:

    function my_action_callback() {
    global $wpdb;
    $names = array();
    $whatever = intval( $_POST['whatever'] );
    //while ( bp_group_members() ) : bp_group_the_member();
    								
    //$name = bp_group_member_name() ;
    //array_push($names, $name);
    //endwhile;
    								
    $rep2 = array( $_POST['whatever'] . ' successful!' );
    							
    echo json_encode($rep2);
    die();
    }

    This stops the click from alerting anything

    function my_action_callback() {
    global $wpdb;
    $names = array();
    $whatever = intval( $_POST['whatever'] );
    while ( bp_group_members() ) : bp_group_the_member();
    $name = bp_group_member_name() ;
    array_push($names, $name);
    endwhile;
    								
    $rep2 = array( $_POST['whatever'] . ' successful!' );
    							
    echo json_encode($rep2);
    die();
    }

    Any idea why is the loop wrong there?

    Any help highly appreciated


    shanebp
    Moderator

    @shanebp

    As I pointed out above, you should use bp_get_group_member_name() not bp_group_member_name().
    The latter will echo out the name. The former will return it as a variable.

    Check your error logs.
    You need to be on a group page to use while ( bp_group_members() )
    Otherwise you need to precede it with if ( bp_group_has_members( 'group_id=15' ) ) :


    Kiciana
    Participant

    @kiciana

    Is this right then?

    function my_action_callback() {
    
    				
    $names = array();
    $whatever = intval( $_POST['whatever'] );
    while ( bp_group_members() ) : bp_group_the_member();
    								
    array_push($names, bp_get_group_member_name());
    								
    								
    endwhile;
    								
    //$rep2 = array( $_POST['whatever'] . ' successful!' );
    							
    echo json_encode($names);
    die();
    }

    The plugin is run only in a group view.


    Kiciana
    Participant

    @kiciana

    Btw from the log

    [06-Dec-2014 18:02:54] PHP Fatal error: Call to a member function members() on a non-object in /home/public_html/wordpress/wp-content/plugins/buddypress/bp-groups/bp-groups-template.php on line 2815

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘group member names push into array not working’ is closed to new replies.
Skip to toolbar