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>';
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.
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
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?
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?
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
grate script ichBin...i really love it... ;)
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.
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"
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?
Did you read the post before yours ?
http://www.tinyportal.net/index.php/topic,30029.msg243587.html#msg243587
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
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"
Shew... Thank you!!!
Thats my lady! G6 to the rescue! :P
I would need to have the names and posts in this order please
Name | Posts
Avatar
Name | Posts
Avatar
Name | Posts
Avatar
Greets
Kimmen
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
"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 :(
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?
Yes :)
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 />';
}
Nice, thanks Deni, that works like a charm :-*
Greets
Kimmen
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?
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 :)
Oh ok, I just wanted to make sure it wasn't just my install. I'll keep messing with it. :)
Thanks.
http://www.tinyportal.net/index.php?topic=31670.0
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?
Ah my bad, I thought that was one I updated for SMF2. I'll have to get to that ASAP.
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. :)
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.
oh ok Thanks IchBin, I appreciate the explanation.
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
Code should be updated in that topic now.
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,,,
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?
Does he hide his online status ?
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?
Very good ! Now it work properly! O0
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>