Hey everybody,

I'm tryinbg to make a block to go along with the buddylist module. The block will show a few random people that the user has marked as a friend, along with their picture. The only problem is, I'm not really sure how I could go about doing this, so I'm lookng for any sugestions/help. I know enough PHP to get around, but not enough to site down and start coding something like this from scratch. Any help would be great.

Mets

Comments

munga’s picture

to display a random user from a specific role (Staff in this case). It should be easy to adapt this code for your use.

<?php
    $result = db_query("SELECT u.uid FROM {users} u LEFT JOIN {users_roles} ur, {role} r ON ur.rid = r.rid AND ur.uid = u.uid WHERE r.name='Staff' AND u.picture LIKE '%%files/pictures%%' ORDER BY RAND() LIMIT 1"); 
    $account = db_fetch_object($result);
    $user = user_load(array('uid' => $account->uid));
    profile_load_profile($account->uid);
?>
<center>
<a href="<?php print $base_path ?>/user/<?php print $user->uid ?>">
<?php print $user->profile_fullname ?></a><br/>
<?php if($user->profile_officer) {print $user->profile_officer; } ?>
<?php if($user->profile_staff) {print $user->profile_staff; } ?>
<div>
<?php if($user->picture): ?>
<div class="picture">
<a href="<?php print $base_path ?>/user/<?php print $user->uid ?>">
<img src="<?php print $base_path ?>/<?php print $user->picture ?>" alt="<?php print $user->profile_fullname ?>" />
</a>
</div>
<?php endif; ?>
</div>
</center>
Mets’s picture

Thanks a million for the help munga - that would have taken me a while to come up with! I've checked the code several times and seem to be able to get it to work in PHP-Eclipse, however, when I try the code that you gave me on the live server, I'm getting this error:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' role r ON ur.rid = r.rid AND ur.uid = u.uid WHERE u.picture LIKE '%files/pictur' at line 1 query: SELECT u.uid FROM users u LEFT JOIN users_roles ur, role r ON ur.rid = r.rid AND ur.uid = u.uid WHERE u.picture LIKE '%files/pictures%' ORDER BY RAND() LIMIT 1 in C:\apachefriends\xampp\htdocs\drupal\includes\database.mysqli.inc on line 121.

I doubt its an SQL syntax problem...any ideas? I actually saw something like this with another code segment at http://drupal.org/node/36822. I'm running 4.7.1

Thanks again, Mets

Mets’s picture

Ok I did some more looking and I put this together:

<?php
/*
* This php snippet loads up a random user object and displays their name
and avatar.
* $count controls (x) number of users to display
* $no_avatar controls the path to the default avatar image to display.
*
* tested and works with drupal 4.6.x
*/
$count = 6;
$no_avatar = "blank_for_now";;
$result = db_query_range(db_rewrite_sql("SELECT * FROM {users} u ORDER BY RAND()"), 0, $count);
  while ($user_info = db_fetch_object($result)) {
   if($user_info->uid){
       if($user->picture) {
               print '<div id="blockpic"><img src="/'.$user->picture.'" width=70></div>';
       }
       else {
               print '<div id="blockpic"><img src="/'.$no_avatar.'" width=70></div>';
       }
       print l($user_info->name, "user/$user_info->uid");
    }
  }
?>

This does a good job of displaying six users, however:

1) it shows 6 users from the entire site - not 6 users from a person's buddy list

2) I'm not sure if they are 6 random users or just the first 6 users who registered on the site

3) the picture is not printing at all. I've played around with it, and the code seems ok, but no pictures show for those users which have a photo

Also, if it would be possible to get it in two columns I'm going to try to do that - probably something with divs - but right now I'm just trying to get 1-3. Any help would be much appreciated!

pkoura’s picture

Hi bud,

Had the exact same problem you did, fiddled with some php, css, altered another drupal user's code, and here's what i got: (works with 4.7)

<div class="friends">
<div id="profileuserlist">
<div class="picContainer2">
<?php
/*
* This php snippet loads up a random user object and displays their name
and avatar.
* $count controls (x) number of users to display
* $no_avatar controls the path to the default avatar image to display.
*
* tested and works with drupal 4.6.x
*/
$count = 5;
$no_avatar = "themes/foliage/buddyicon.jpg";;
$result = db_query_range(db_rewrite_sql("SELECT * FROM {users} u ORDER BY RAND()"), 0, $count);
  while ($user_info = db_fetch_object($result)) {
   if($user_info->uid){
       if($user_info->picture) {
               print '<div id="blockpic"><a href="/user/'.$user_info->uid.'"<img src="/'.$user_info->picture.'" width=50></div>';
       }
       else {
               print '<div id="blockpic"><a href="/user/'.$user_info->name.'"<img src="/'.$no_avatar.'" width=70></div>';
       }

    }
  }
?></div></div></div></a>
zuernbernhard’s picture

How to get this working in Drupal 6 ?