List of friends function not working

Is there an easy way to display a list of my friends?

get_user_friends() does not work or maybe i am doing something wrong, either way can someone please explain to me how to get a list of online friends in one list?

  • Can anyone give me a demonstration of how to display a list of my friends please?

  • To get your friends

    $options = array(
    'relationship' => 'friend',
    'relationship_guid' => $user_guid,
    'inverse_relationship' => FALSE,
    'type' => 'user',
    'full_view' => FALSE
    );
    $content = elgg_get_entities_from_relationship($options);

    If you want to list them, use elgg_list_entities_from_relationship()

  • Thanks Team Webgalli, but i already tried the above from the reference. But i couldn't see any friends.

    Could you provide me with a source sample that will work? I would be very greatful.

    Mucker

  • get_user_friends() will only retrieve a user's friends but can not detect if they are currently online or not.

    Check this plugin: http://community.elgg.org/plugins/1515392/1.8.1/who-are-onlinefriends-online. While I don't know if this plugin will fully work on the latest release of Elgg there is at least the code included to retrieve a user's friends, find out who of the friends is online and display these. The code is within the file friends_online/views/default/friends_online/friendson.php.

  • Thank you iionly i have already tied that too. :)

    The code that you refer to is below, but it also includes the menu for ban, make staff etc... is there a way to remove that? I just want a list of friends names who are online. Here is the code you refer too:

    $friends = $_SESSION['user']->getFriends("",1000);

    $friends_online = 0;
        if (count($friends) > 0)
        {
            foreach ($friends as $friend)
            {
                if ($friend->last_action > time() - 200)
                {        
                    $icon = elgg_view("profile/icon", array('entity' => get_user($friend->guid), 'size' => 'tiny'));
                    echo "<div class=\"friends_online\">\n";     
                    echo $icon;
                    echo "</div>\n";
                    $friends_online++;
                }
            }
        }

  • Not tested but try:

    $friends = elgg_get_logged_in_user_entity()->getFriends("",1000);

    $friends_online = 0;
        if (count($friends) > 0)
        {
            foreach ($friends as $friend)
            {
                if ($friend->last_action > time() - 200)
                {        
                    $icon = elgg_view_entity_icon(get_user($friend_guid), 'size' => 'tiny', array('use_hover' => false));
                    echo "<div class=\"friends_online\">\n";     
                    echo $icon;
                    echo "</div>\n";
                    $friends_online++;
                }
            }
        }

  • Thanks iionly, any idea how to fix the syntax T_DOUBLE_ARROW error on: $icon = elgg_view_entity_icon(get_user($friend_guid), 'size' => 'tiny', array('use_hover' => false));

    And i will let you know if it works for me.

  • 'size' => 'tiny'

    should be just 'tiny'

     

    That's not a good way to get your online friends though.  Consider what happens if you had 1000 friends?

    Even assuming you could get all 1000 of them from the database without exceeding your memory limits, then you have to iterate through all of them to find the potentially only one or two that are online.  Very inefficient.

    $dbprefix = elgg_get_config('dbprefix');

    $time = time() - 200;

    $options = array(

           'relationship' => 'friend',

           'relationship_guid' => $user_guid,

           'type' => 'user',

    'limit' => FALSE,

           'joins' => array("JOIN {$dbprefix}users_entity ue ON e.guid = ue.guid"),

           'wheres' => array("ue.last_action > {$time}")

    );


    $online_friends = elgg_get_entities_from_relationship($options);

     

    As a general rule though you shouldn't try to get all of anything unless you can be sure there's going to be a manageable number of results, or you're using ElggBatch and don't mind a potential wait.  Instead build interfaces that use pagination and/or ajax for showing incremental results.

  • Thanks for the help Matt. The code is written perfect but doesn't output any results or friends names on the page just yet. What am i missing??? 

  • The code Matt given is to get all online friends. If you want to list them use

    echo $online_friends = elgg_list_entities_from_relationship($options);

    or iterate through each online friends and then output the required data

    $online_friends = elgg_get_entities_from_relationship($options);
    foreach($online_friends as $friend){
    echo $friend->name."<br>";
    }