Log in

list random friends

  • Public

list random friends

Started by R 447 days ago Replies (11)

Hello, I have this code:

    $friends = $owner->listFriends('', $num, array(
        'size' => 'tiny',
        'pagination' => FALSE,
        'list_type' => 'gallery',
        'gallery_class' => 'elgg-gallery-users',
        'count' => TRUE
        ));

If I set $num to say 10, is it possible to return 10 random friends instead of the same 10 everytime? I know this is possible, just not sure how to do it. Thanks.

Replies

  • Team Webgalli 447 days ago

    If you set $num = 10, it will return your last ten friends. If you want to show random users, first get more than 10 of them with above function, and get 10  random users from them.

  • R 447 days ago

    Thanks for your repsonse.

    So if a user has 300 friends and I want to grab 10 random ones, I have retrieve all 300 friends and then use php array_rand() to list 10 random ones?

    This seems inefficient to retrieve all 300 to list 10.

  • Team Webgalli 447 days ago

    You can get 15 of them and show random from them.

  • R 447 days ago

    Wont that just shuffle that same 15 friends over and over?

  • Team Webgalli 447 days ago

    Rob, thats the quick solution. if you need a more stable and powerful way, you have to write your own SQL for fetching random users from DB and and then join them with "friend" relationship with your user. Have a look at the engine/lib/users.php file.

  • gillie 446 days ago

    You could also get around it by using random $offset, which is where in the array to start. If you want to list 10 and have 300,

    $num = 10;

    $friends = $owner->listFriends('', $num, array(
        'size' => 'tiny',
        'pagination' => FALSE,
        'list_type' => 'gallery',
        'gallery_class' => 'elgg-gallery-users',
        'offset' => rand(0, 290),
        'count' => TRUE
    ));

     

  • R 445 days ago

    @gillie

    I like this option, I think I will try this out. One more things though. Is there an easy way to count the total number of friends a user has?

  • gillie 445 days ago

    Might be an easier way, but you can use something like this,

    $options = array(
        'relationship' => 'friend',
        'relationship_guid' => elgg_get_page_owner_guid(),
        'types' => 'user',
        'count' => true
    );
    $count = elgg_get_entities_from_relationship($options);

  • Ismayil Khayredinov 445 days ago

    You can simply use 'order_by' => 'rand()' 

  • gillie 445 days ago

    That was easy Ismayil, thanks.

  • R 445 days ago

    @ Ismayil

    Great I knew there was a simple solution! I will try this out.

    @ gillie

    Thanks for your suggestions!

You must log in to post replies.