TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 884
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 332
  • Total: 332

Could someone modify the top posters snippet into weekly top posters?

Started by shuban, October 02, 2011, 07:38:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

shuban

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?

BradIsBad

Yes, I would really like to be able to post a time limit too but I have no clue how to do that either.

IchBin

I merged my code with the code that SiNaN at simple portal posted for the top poster in the week. Basically all I needed was to figure the time since Sunday, and to calculate posters since that time. He had done the work in his query, so I merged it with mine to grab the avatars. This should work, assuming you have the no avatar image from that post linked to above for users with no avatar.

Credits goes to SiNaN for the hard work. I did the easy part. ;)

global $smcFunc, $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);
// Limit of top posters
$limit = 8;
// Height and width of avatar
$width = '40px';
$height = '40px';

$request = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, COUNT(*) as count_posts, mem.avatar, a.id_attach, a.attachment_type, a.filename
FROM {db_prefix}messages AS m
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
WHERE m.poster_time > {int:start_time}
AND m.id_member != 0
AND mem.show_online = 1
GROUP BY mem.id_member
ORDER BY count_posts DESC
LIMIT {int:limit}',
array(
'start_time' => $starttime,
'limit' => $limit
)
);

$context['top_posters'] = array();
$max_num_posts = 1;
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$context['top_posters'][] = array(
'name' => $row['real_name'],
'id' => $row['id_member'],
'num_posts' => $row['count_posts'],
'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
'avatar' => array(
'image' => empty($row['avatar']) ? ($row['id_attach'] > 0 ? 'src="' . (empty($row['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $row['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />' : '') : (stristr($row['avatar'], 'http://') ? 'src="' . $row['avatar'] . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />' : 'src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($row['avatar']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />'),
),
);

if ($max_num_posts < $row['count_posts'])
$max_num_posts = $row['count_posts'];
}
$smcFunc['db_free_result']($request);

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

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

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

foreach ($context['top_posters'] 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['num_posts'] . '</h5></td>
</tr>';
}

echo '
</table>';

This website is proudly hosted on Crocweb Cloud Website Hosting.