Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

October 20, 2021, 12:30:01 AM

Login with username, password and session length
Members
  • Total Members: 3825
  • Latest: JLavau
Stats
  • Total Posts: 192534
  • Total Topics: 21007
  • Online Today: 44
  • Online Ever: 1035
  • (November 26, 2020, 04:45:12 AM)
Users Online
Users: 0
Guests: 46
Total: 46

Author Topic: [Block] Users Online with Avatar  (Read 7634 times)

0 Members and 1 Guest are viewing this topic.

Offline IchBin

  • Developer
  • *
  • Posts: 16231
[Block] Users Online with Avatar
« on: January 11, 2010, 02:09:19 PM »
Name of Snippet: Users Online with Avatar
SMF/TP versions tested:: SMF1 & SMF2/TP1 beta 4
Block Type:: php
Author: IchBin
Link to Discussion: http://www.tinyportal.net/index.php?topic=27231.0
Other Requirements: None
Description:
This php code snippet will only work in a phpbox type block. It has a configurable column setting so it can fit a top/bottom or side block. You can also set a time limit on how far back you want to show users online i.e. 15 minutes, 1hour, 1day. All of the configurable options are put in the top, including any CSS changes you want to make. If your user avatars are larger or smaller than 100x100 pixels wide, adjust the CSS accordingly in the code. A default avatar is set if the user does not have an avatar. This assumes there is a file named noavatar.gif (attached) in your themes images directory.

Code For SMF1.x
Code: [Select]
global $db_prefix, $scripturl, $modSettings;

/* ###  CONFIGURATION OPTIONS  ### */
// Set time limit using seconds
// 1 hour = 3600
// 1 day = 84600
$timelimit = 3600;

// Set $columns to the amount of columns you need
// You set this depending on whether you want it in a side block or a top/bottom block.
$columns = 5;

// Set the height and width of avatars
// This will resize the avatars to be more uniform
$width = '40px';
$height = '40px';

// Set or change the style of all the elements
echo '<style type="text/css">
.avatar_column{
border: 0;
}
.avatar_column td{
height: 100px;
width: 100px;
overflow: hidden;
text-align: center;
vertical-align: top;
}
.default{
border: 2px solid black;
}
.he{
border: 2px solid blue;
}
.she{
border: 2px solid pink;
}
</style>';

/* ###  END CONFIGURATION OPTIONS  ### */

// Load all users who have logged in within $timelimit
$result = db_query("
SELECT mem.ID_MEMBER, mem.showOnline, mem.lastLogin, mem.realName, mem.avatar, mem.gender, 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 mem.lastLogin > (UNIX_TIMESTAMP() - $timelimit)
ORDER BY mem.lastLogin DESC", __FILE__, __LINE__);

$users = array();

// Loop through the results to display the users avatar
while ($row = mysql_fetch_assoc($result)){
    $users[$row['ID_MEMBER']] = array (
    'id' => $row['ID_MEMBER'],
    'href' => $scripturl.'?action=profile;u='.$row['ID_MEMBER'],
    'name' => $row['realName'],
    'show' => $row['showOnline'],
'gender' => $row['gender'],
    'avatar' => array(
    'image' => $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($result);

echo '
<table class="avatar_column">
<tr>';

$counter = 0;

foreach ($users as $user){
if ($user['show'] == 1){
switch ($user['gender']){
case 0:
$css = "default";
break;
case 1:
$css = "he";
break;
case 2:
$css = "she";
break;
}
if ($counter < $columns){
echo '
    <td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
    $counter++;
}
else{
echo '
</tr>
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
$counter = 1;
}
}
}
echo '
</tr>
</table>';
« Last Edit: January 15, 2010, 06:29:43 PM by IchBinâ„¢ »

Offline IchBin

  • Developer
  • *
  • Posts: 16231
Re: [Block] Users Online with Avatar
« Reply #1 on: January 15, 2010, 06:30:43 PM »
Code for SMF2
Code: [Select]
global $smcFunc, $db_prefix, $scripturl, $modSettings, $settings;

/* ###  CONFIGURATION OPTIONS  ### */
// Set time limit using seconds
// 1 hour = 3600
// 1 day = 84600
$timelimit = 3600;

// Set $columns to the amount of columns you need
// You set this depending on whether you want it in a side block or a top/bottom block.
$columns = 5;

// Set the height and width of avatars
// This will resize the avatars to be more uniform
$width = '40px';
$height = '40px';

// Set or change the style of all the elements
echo '<style type="text/css">
.avatar_column{
border: 0;
}
.avatar_column td{
height: 100px;
width: 100px;
overflow: hidden;
text-align: center;
vertical-align: top;
}
.default{
border: 2px solid black;
}
.he{
border: 2px solid blue;
}
.she{
border: 2px solid pink;
}
</style>';
/* ###  END OF CONFIGURATION OPTIONS  ### */

$mdate = date("U");

// Load all users who have logged in within $timelimit
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.show_online, mem.last_login, mem.real_name, mem.avatar, mem.gender, a.id_attach, a.attachment_type, a.filename
FROM smf_members AS mem
LEFT JOIN smf_attachments AS a ON (a.id_member = mem.id_member)
WHERE mem.last_login > ({int:date} - '.$timelimit.')
ORDER BY mem.last_login DESC',
array('date' => $mdate)
);

$users = array();

// Loop through the results to display the users avatar
while ($row = $smcFunc['db_fetch_assoc']($result)) {
    $users[$row['id_member']] = array (
    'id' => $row['id_member'],
    'href' => $scripturl.'?action=profile;u='.$row['id_member'],
    'name' => $row['real_mame'],
    'show' => $row['show_online'],
'gender' => $row['gender'],
    'avatar' => array(
    'image' => $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'].'" />'),
    ),
    );
}

$smcFunc['db_free_result']($result);

echo '
<table class="avatar_column">
<tr>';

$counter = 0;

foreach ($users as $user){
if ($user['show'] == 1){
switch ($user['gender']){
case 0:
$css = "default";
break;
case 1:
$css = "he";
break;
case 2:
$css = "she";
break;
}
if ($counter < $columns){
echo '
    <td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
    $counter++;
}
else{
echo '
</tr>
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
$counter = 1;
}
}
}
echo '
</tr>
</table>';
« Last Edit: March 06, 2010, 07:17:18 PM by IchBinâ„¢ »

Offline Renegd98

  • Hero Member
  • *****
  • Posts: 733
Re: [Block] Users Online with Avatar
« Reply #2 on: March 06, 2010, 06:39:00 AM »
IchBin,

You need to add $settings to the global define line... or .$settings throws and error.

Offline IchBin

  • Developer
  • *
  • Posts: 16231
Re: [Block] Users Online with Avatar
« Reply #3 on: March 06, 2010, 07:17:43 PM »
Done