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.
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.
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.
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.
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
));
@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?
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
Great I knew there was a simple solution! I will try this out.
@ gillie
Thanks for your suggestions!