TinyPortal

Development => Block Codes => Topic started by: IchBin on June 22, 2009, 08:30:58 PM

Title: [Block] Top Posters with avatar
Post by: IchBin on June 22, 2009, 08:30:58 PM
Top Posters block. Made for a vertical layout only. This block will show the avatar, username, and post count of the Top Posters on your site. You can select the size that you want the avatars to be display by changing the $width/$height variables at the top. You can also change how many users you want to show by changing the $topPoster variable. There is also a noavatar.gif posted below that you will need to use, because this code shows a default avatar for users who don't have one.

global $db_prefix, $scripturl, $modSettings;

// Height and width of avatar
$width = '40px';
$height = '40px';
// Number of top posters displayed
$topPoster = 8;

// Find the latest poster.
$request = db_query("SELECT mem.ID_MEMBER, mem.showOnline, mem.realName, mem.posts, mem.avatar, a.ID_ATTACH, a.attachmentType, a.filename
FROM ({$db_prefix}members as mem)
LEFT JOIN {$db_prefix}attachments AS a ON (a.ID_MEMBER = mem.ID_MEMBER)
WHERE showOnline = 1
ORDER BY posts DESC
LIMIT $topPoster", __FILE__, __LINE__);

$users = array();

while ($row = mysql_fetch_assoc($request))
{
$users[] = array(
'id' => $row['ID_MEMBER'],
'name' => $row['realName'],
'href' => $scripturl . '?action=profile;u=' . $row['ID_MEMBER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['realName'] . '</a>',
'posts' => $row['posts'],
'show' => $row['showOnline'],
'avatar' => array(
    'image' => empty($row['avatar']) ? ($row['ID_ATTACH'] > 0 ? 'src="' . (empty($row['attachmentType']) ? $scripturl . '?action=dlattach;attach=' . $row['ID_ATTACH'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />' : '') : (stristr($row['avatar'], 'http://') ? 'src="' . $row['avatar'] . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />' : 'src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($row['avatar']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />'),
    ),
);
}

mysql_free_result($request);

// Output our array of users with avatar, posts, and name
echo '
<table cellpadding="0" cellspacing="8">';

foreach ($users as $user)
{
echo '
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a>' : '<a href="'.$user['href'].'"><img '.$user['avatar']['image'].'</a>';
echo '
</td>
<td><h5 style="margin: 4px;">'.$user['link'].'</h5><h5 style="margin: 4px;">'. $user['posts'] .'</h5></td>
</tr>';
}

echo '
</table>';

Title: Re: [Block] Top Posters with avatar
Post by: Herr Inoddorell on June 22, 2009, 08:58:04 PM
I've attached a pic of the block from my site, for everyone to see how it looks. I included the "Posts" word next to the number of posts, set the $topPoster variable to 5, changed the $width/$height to 50px, and changed the margin of the "h5 style" entry to 5px.

BTW, I've noticed that in the code of the OP, a bit disalignment between the user name and the total post they made. This issue get fixed by changing the first <h5style="margin: 4px;"> entry to <h5 style="margin: 4px;">. <-- Well done, Ich LOL).

Hope everyone enjoy this code :). Great work Ich! ;)!

EDIT: Also attached the "noavatar.gif" image, if you need it.
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on June 22, 2009, 11:23:44 PM
Fixed the code. Don't ask me how I didn't fix that last time. Upgrade at work, doing to many things at once. lol
Title: Re: [Block] Top Posters with avatar
Post by: jabisai on July 10, 2009, 12:33:58 AM
Sorry but how i upload this block to my site??
'cos I try to put in the "Panels and Blocks" and then "Add left block" and I paste your code in php form.

But I can see the block in the left panel?
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on July 10, 2009, 03:21:50 AM
What version of TP are you using? When you edit the block again, is all the code there? Are you using a PHP block type?
Title: Re: [Block] Top Posters with avatar
Post by: ZarPrime on July 10, 2009, 01:58:15 PM
Besides what Ichbin said, have you set where the block should show?  At the bottom of the block edit screen, where you see "[Show][Hide]", be sure to click [Show] to expand it so that you can select where to show the block.

See enclosed pic below.

ZarPrime
Title: Re: [Block] Top Posters with avatar
Post by: tigerclaw on July 19, 2009, 06:54:30 AM
grate script ichBin...i really love it... ;)
Title: Re: [Block] Top Posters with avatar
Post by: vas on August 13, 2009, 04:05:39 AM
nomage.gif gets stored in the themes image dir. Because of that the noimage.gif has to be laoded in each theme dir. Is there a way to store in the forum image directory so that the image loading can be centralized. Thanks.
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on August 13, 2009, 05:35:13 AM
Yep, just change the path on the image.
<img src="'.$settings['images_url'].'/noavatar.gif"

Replace this text '.$settings['images_url'].' with the path to your image that you want.

<img src="'/home/user/www/images/noavatar.gif"

Title: Re: [Block] Top Posters with avatar
Post by: dkharp on October 02, 2009, 08:35:21 AM
greetings!

Block is up and works good! 

This is the path to my pic. www.36thid.info//Themes/default/images/ImagesOnBoard/noavatar.gif

Where would I put this?
Title: Re: [Block] Top Posters with avatar
Post by: G6Cad on October 02, 2009, 09:09:22 AM
Did you read the post before yours ?

http://www.tinyportal.net/index.php/topic,30029.msg243587.html#msg243587
Title: Re: [Block] Top Posters with avatar
Post by: dkharp on October 02, 2009, 09:19:34 AM
Yes Sir,

But I am a bit confused. the /www/ part has me for a loop.

I have tried this: src="'Themes/default/images/ImagesOnBoard/noavatar.gif/noavatar.gif"

and this www.36thid.info//Themes/default/images/ImagesOnBoard/noavatar.gif

Title: Re: [Block] Top Posters with avatar
Post by: G6Cad on October 02, 2009, 09:38:03 AM
Just add the images in the /themes/default/images folder and your other themes image folder if you use this code

<img src="'.$settings['images_url'].'/noavatar.gif"
Title: Re: [Block] Top Posters with avatar
Post by: dkharp on October 02, 2009, 09:42:51 AM
Shew... Thank you!!!
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on October 02, 2009, 03:58:52 PM
Thats my lady! G6 to the rescue! :P
Title: Re: [Block] Top Posters with avatar
Post by: Kimmen on October 06, 2009, 09:31:08 PM
I would need to have the names and posts in this order please

Name | Posts
Avatar

Name | Posts
Avatar

Name | Posts
Avatar


Greets
Kimmen
Title: Re: [Block] Top Posters with avatar
Post by: ZarPrime on October 06, 2009, 10:22:47 PM
Kim,

This is just a matter of modifying the table at the bottom  the code in the OP.  You should certainly be able to do that by now, right?  ;) Of course, I don't know how it will look if you have anyone with a long username.

ZarPrime
Title: Re: [Block] Top Posters with avatar
Post by: Kimmen on October 06, 2009, 10:27:45 PM
"You should certainly be able to do that by now, right?"

Yeah right, thanks for the motivation  ;D

But no, im still a nooob at this :( 
Title: Re: [Block] Top Posters with avatar
Post by: JPDeni on October 06, 2009, 11:31:17 PM
So you want the username, then a pipe character, then the number of posts. And below that you want the avatar. Then you want two blank lines and the same thing for the next person. Yes?
Title: Re: [Block] Top Posters with avatar
Post by: Kimmen on October 06, 2009, 11:51:14 PM
Yes  :)
Title: Re: [Block] Top Posters with avatar
Post by: JPDeni on October 07, 2009, 12:06:00 AM
This is the code that prints out the information and it's what you're going to need to change:



// Output our array of users with avatar, posts, and name
echo '
<table cellpadding="0" cellspacing="8">';

foreach ($users as $user)
{
echo '
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a>' : '<a href="'.$user['href'].'"><img '.$user['avatar']['image'].'</a>';
echo '
</td>
<td><h5 style="margin: 4px;">'.$user['link'].'</h5><h5 style="margin: 4px;">'. $user['posts'] .'</h5></td>
</tr>';
}

echo '
</table>';


And this is what you would change it to



foreach ($users as $user)
{
echo $user['link'] , ' | ' , $user['posts'] , '<br />' ,
                       empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a>' : '<a href="'.$user['href'].'"><img '.$user['avatar']['image'].'</a>';
                 echo '<br /><br />';
}
Title: Re: [Block] Top Posters with avatar
Post by: Kimmen on October 07, 2009, 10:39:39 AM
Nice, thanks Deni, that works like a charm  :-*

Greets
Kimmen
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 13, 2010, 06:21:39 AM
Hi All, I had tried this code out on SMF 1.1.10 TP 1 beta5 and it worked great.
but the other day, I upgraded to SMF 2.0 RC3 and TP 1 beta 5-1 and I set up a block to use this code, but it didn't work.  Is there some new formatting that needs to be done with this now to work with new tp beta 5-1?
Title: Re: [Block] Top Posters with avatar
Post by: G6Cad on April 13, 2010, 06:31:16 AM
Yes, it needs updating as SMF have changed a lot between the versions from 1.1 to rc3
Cant tell you exactly what to change though as i dont know, hopefully one of the coders or the origin block code creator can help you with it :)
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 13, 2010, 06:44:40 AM
Oh ok, I just wanted to make sure it wasn't just my install.  I'll keep messing with it.  :)
Thanks.
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on April 13, 2010, 05:00:47 PM
http://www.tinyportal.net/index.php?topic=31670.0
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 13, 2010, 05:43:20 PM
I tried the code in that link that you pasted Ichbin, but it still didnt work.  I see that it says "only tested on 1.1.11",

I have played with the code forever and not understanding why it would not work. Does anyone know Whats so different about the new php blocks that make old code not work?



Title: Re: [Block] Top Posters with avatar
Post by: IchBin on April 13, 2010, 07:48:48 PM
Ah my bad, I thought that was one I updated for SMF2. I'll have to get to that ASAP.
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 13, 2010, 07:54:15 PM
thanks man, that Top Posters is a great code. 
What is it that has to be updated with the php codes?  Just curious because it seems like a few are not working. Is it because variables have different names now? and thats why they dont work? Im just trying to learn this. :) 
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on April 13, 2010, 08:20:18 PM
Depends on the code, but generally the differences are the way SMF2 makes the database calls. They've also change $txt strings as well as table row names. Things like realName have become real_name for compatibility with the other databases they now support.
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 13, 2010, 08:26:44 PM
oh ok Thanks IchBin, I appreciate the explanation.
Title: Re: [Block] Top Posters with avatar
Post by: ZarPrime on April 14, 2010, 01:33:45 AM
Shane,

If you're really interested in the changes in SMF 2.0, take a seat and have a read of this topic --> http://docs.simplemachines.org/index.php?topic=1433

This will give you a at least a basic understanding of some of the changes.

ZarPrime
Title: Re: [Block] Top Posters with avatar
Post by: IchBin on April 14, 2010, 03:07:02 AM
Code should be updated in that topic now.
Title: Re: [Block] Top Posters with avatar
Post by: shane is my name on April 14, 2010, 04:11:10 AM
Thanks IchBin,  it's a great code. Im going to try it again now.

and Thanks for the link ZarPrime.  yeah i am interested. Im always trying to learn more..  Im always buying books etc trying to do this and that.. and I thought I was kind of good, until I started using smf and TP,  and now im lost at every turn. lol,  but one of these days,,, 
Title: Re: [Block] Top Posters with avatar
Post by: shuban on June 06, 2011, 04:40:19 PM
It's very strange, but one of my posters who is the top 10 isn't being shown in this list. What do you think could be the reason why this one user isn't being shown?
Title: Re: [Block] Top Posters with avatar
Post by: Freddy on June 06, 2011, 04:51:45 PM
Does he hide his online status ?
Title: Re: [Block] Top Posters with avatar
Post by: shuban on October 02, 2011, 07:40:04 PM
Hi everyone,

I really like this snippet's layout:

http://www.tinyportal.net/index.php?topic=31670.0

Although I want it to display the top posters in past 7 days (i.e. in the last week).

Could someone please modify it for me?
Title: Re: [Block] Top Posters with avatar
Post by: saks on October 03, 2011, 05:25:18 AM
Very good ! Now it work properly!  O0
Title: Re: [Block] Top Posters with avatar
Post by: shuban on October 03, 2011, 11:02:55 PM
Quote from: shuban on October 02, 2011, 07:40:04 PM
Hi everyone,

I really like this snippet's layout:

http://www.tinyportal.net/index.php?topic=31670.0

Although I want it to display the top posters in past 7 days (i.e. in the last week).

Could someone please modify it for me?

Okay, I've got it down to this, but I do not know how to include an avatar! :(

global $db_prefix, $scripturl, $context, $settings;

// Top 10 Posters so far this week  (starts sunday)
$starttime = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N")*3600*24);
// Offset based on forum time
$starttime = forum_time(false, $starttime);

$request = db_query("
SELECT me.ID_MEMBER, me.realName, COUNT(*) as count_posts
FROM {$db_prefix}messages AS m
LEFT JOIN {$db_prefix}members AS me ON (me.ID_MEMBER = m.ID_MEMBER)
WHERE m.posterTime > " . $starttime . "
AND m.ID_MEMBER != 0
GROUP BY me.ID_MEMBER
ORDER BY count_posts DESC
LIMIT 7", __FILE__, __LINE__);

$context['top_posters_week'] = array();
$max_num_posts = 1;
while ($row_members = mysql_fetch_assoc($request))
{
$context['top_posters_week'][] = array(
'name' => $row_members['realName'],
'id' => $row_members['ID_MEMBER'],
'num_posts' => $row_members['count_posts'],
'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
'avatar' => array(
    'image' => empty($row['avatar']) ? ($row['ID_ATTACH'] > 0 ? 'src="' . (empty($row['attachmentType']) ? $scripturl . '?action=dlattach;attach=' . $row['ID_ATTACH'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="" border="0" width="30px" height="30px" title="'.$row['realName'].'" />' : '') : (stristr($row['avatar'], 'http://') ? 'src="' . $row['avatar'] . '" alt="" border="0" width="30px" height="30px" title="'.$row['realName'].'" />' : 'src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($row['avatar']) . '" alt="" border="0" width="30px" height="30px" title="'.$row['realName'].'" />'),
    ),
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>'
);

if ($max_num_posts < $row_members['count_posts'])
$max_num_posts = $row_members['count_posts'];
}
mysql_free_result($request);

foreach ($context['top_posters_week'] as $i => $j)
$context['top_posters_week'][$i]['post_percent'] = round(($j['num_posts'] * 100) / $max_num_posts);

// Tidy up
unset($max_num_posts, $row_members, $j, $i);

echo '
<table cellpadding="0" cellspacing="4">';

foreach ($context['top_posters_week'] as $poster)
echo '
<tr>

<td style="overflow: hidden;"><a href="'.$poster['href'].'"><img '.$poster['avatar']['image'].'</a></td>

<td style="overflow: hidden;"><h5 style="margin: 4px;">', $poster['link'], '</h5><h5 style="margin: 4px;">', $poster['num_posts'], '</h5></td>
</tr>';
echo '
</table>';


Please help! I think it's here:

$request = db_query("
      SELECT me.ID_MEMBER, me.realName, COUNT(*) as count_posts
      FROM {$db_prefix}messages AS m
         LEFT JOIN {$db_prefix}members AS me ON (me.ID_MEMBER = m.ID_MEMBER)


That I have to include something about avatars in order for this part (below), to work!

<td style="overflow: hidden;"><a href="'.$poster['href'].'"><img '.$poster['avatar']['image'].'</a></td>