Hey guys,

I'm having a little trouble modifying the "Who's New" block. I would like to show people in this block, only if they have uploaded an avitar. Here is the related code from user.module.

case 2:
        if (user_access('access content')) {
          $result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 5);
          while ($account = db_fetch_object($result)) {
            $items[] = format_name($account);
          }

          $output = theme('user_list', $items);

          $block['subject'] = t('Who\'s new');
          $block['content'] = $output;
        }
        return $block;

Could someone show me how to add if($user->picture).

If possible, could I also display the avitar next to the name?

Any help would be so much appreciated. THANKS!

Comments

bradlis7’s picture

I think it would be best to change the SQL statement.

Change the following:

$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 5);

to this:

$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 AND picture != "" ORDER BY uid DESC', 0, 5);
potential’s picture

Thanks that worked well. Do you if its possible to show the avitar along with the name?

bradlis7’s picture

I would suggest changing the following:

  $items[] = format_name($account);

by adding the image either before or after the username (only pick one):

  $items[] = "<img src='$account[picture]'>" . format_name($account);
  $items[] = format_name($account) . "<img src='$account[picture]'>";


You will also have to get the picture in the mysql statement. Change:

$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 5);

to:

$result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 5);
potential’s picture

Hi Brad,

Thanks for the help man. Unforunatly, I got an error when I tried that.

Fatal error: Cannot use object of type stdClass as array in F:\www\htdocs\modules\user.module on line 535

I know we are close tho.

Here is some code I found elsewhere and modified. It almost works perfectly.

<?php
$result = db_query_range(db_rewrite_sql("SELECT * FROM {users} u WHERE status != 0 AND picture != '' ORDER BY uid DESC"), 0, 4);
  while ($user_info = db_fetch_object($result)) {
   if($user_info->uid){
       print '<div id="blockpic">
       <a href="/user/'.$user_info->uid.'"><img src="/'.$user_info->picture.'" width=150 /a>
       </div>
       ';
       print $user_info->name;
    }
  }
?> 

I don't think I'm using the correct method to link to the profile, and I also think my php syntax is off somewhere. Can you take a look at my code and let me know how to improve it?

THANKS!

bradlis7’s picture

Your example code helped me. I thought that $user_info was an array, but using the "->" is correct. I'm crossing my fingers this time. This is just modified from the original.

        if (user_access('access content')) {
          $result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND picture != "" ORDER BY uid DESC', 0, 5);
          while ($account = db_fetch_object($result)) {
            $items[] = '<img src="/'.$user_info->picture.'">'.format_name($account);
          }
          $output = theme('user_list', $items);
          $block['subject'] = t('Who\'s new');
          $block['content'] = $output;
        }
        return $block;

If you'd like to use something like the one you found, I'd suggest doing it the following way:

$result = db_query_range(db_rewrite_sql("SELECT uid,name,picture FROM {users} u WHERE status != 0 AND picture != '' ORDER BY uid DESC"), 0, 4);
  while ($user_info = db_fetch_object($result)) {
   if($user_info->uid){
       echo '<div id="blockpic"><a href="/user/'.$user_info->uid.'">';
       echo '<img src="/'.$user_info->picture.'" width=150>';
       echo $user_info->name . '</a></div>';
    }
  }

Selecting only the needed fields in the SQL statement will improve performance. The html also needed some adjustment.

See if you can get one of these to work and let me know how it works out.

potential’s picture

Thanks Brad,

I used the second set of code and its working wonderfully.

If you have a chance, can you take a look at another php question I have. It hasn't received a response in a few days.

http://drupal.org/node/46461

Thanks for everything so far. My site looks much better already.

bombaclot’s picture

I think it's a path issue. It wants to show an image, but it's showing a broken image with a red x. what the ...am I doing wrong?
I posted this exact code in in case 2 on user.module
It's fine except the broken image...does this have to do with my drupal being in a subdirectory? help please. Thank you.

if (user_access('access content')) {
$result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND picture != "" ORDER BY uid DESC', 0, 5);
while ($account = db_fetch_object($result)) {
$items[] = 'Only local images are allowed.picture.'">'.format_name($account);
}
$output = theme('user_list', $items);
$block['subject'] = t('Who\'s new');
$block['content'] = $output;
}
return $block;

bradlis7’s picture

I don't know exactly, but what you could do is find out the path of the image it's giving you. In firefox, right click->properties, and it should be the first item. That's where I'd start debugging. If you can't figure out, tell me what the image is pointing to and where it should point to.

--
Bradlis7.com | Churchofchristnet

bombaclot’s picture

I got it to work! I appreciate it. Used the same snippet above. If you have any ideas about getting an image to show next the username in the buddylist on the profile page, that's what I'm up to now.
Thanks again.

lando@getnix.com’s picture

Interestingly enough I had to combine the MySql query from the upper snippet with the code from the lower one.

I created a custom block and added

 $result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND picture != "" ORDER BY uid DESC', 0, 5);
  while ($user_info = db_fetch_object($result)) {
  if($user_info->uid){
      echo '<div id="blockpic"><a href="/user/'.$user_info->uid.'">';
      echo '<img id="new-people" src="/'.$user_info->picture.'"><br />';
      echo $user_info->name . '</a></div>';
    }
  }

Here is the CSS.
#blockpic, #blockpic a{
text-align: center;
text-decoration: none;
}
#new-people{
width: 50px;
width: 50px;
}

I was getting a nasty MySql error whenever I logged out using the lower snippet from http://drupal.org/node/46619#comment-88124 . After several hours of digging I came across a thread dealing with a similar issue http://drupal.org/node/43735 . The gist of that thread was that the db_rewrite_sql() function was causing issues. For the detail hungry folks see http://drupal.org/node/43735#comment-92865 . Thank you both for putting this together I hope someone needing the same feature will benefit from my Franken-snippet. ;)