News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Fetching avatars

Started by MentaL, October 30, 2009, 03:39:30 PM

Previous topic - Next topic

MentaL

So, I'm working on elaborating my webpage and making it more .. Dynamical than it's current much statical look.

One of the things I was thinking about doing, when displaying recent posts on the front-page using a customized ssi_boardNews() array, is there any way to fetch the posters avatar in some way and using the stored variable and echo it out on the frontpage?

I've been looking around for some time about this but I haven't been able to figure out of it.
With the exception being using $news['poster']['id'] and creating a MySQL script to fetch the attachment ID directly from the {table prefix}_attachments, then creating a URL filling in index.php?action=dlattach;attach={$user_attachment_ID};type=avatar

Though that looks like a really dirty "quick fix" for my problem, and not very dynamical at that.

Does anyone have any idea? :) Any help would be appreciated!

Cheers,
- MentaL

Kays


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

MentaL

Tried following that topic but it really didn't help me much sadly. I'll keep on trying but I'm in doubt about my abilities on that.  :'(

Kays

Or my abilities to describe something properly.

That example used ssi_recentTopics. It's slightly different for ssi_boardNews. Where are you having a problem?

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

MentaL

Well I'm having slight difficulties understanding the script as a whole.  I've tried editing it to be usable in ssi_boardNews but it doesn't seem to load at all so I gave up in the end.

Tried loading it as is but started getting various "..Cannot use string offset as an array.." messages.

Kays


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

spottedhog

Here is a function I created for my PHP Nuke/SMF integration:  (Note--copy/pasting the entire code will not work directly in SMF since it contains the PHP Nuke database calls)
// This function will display the Avatar from SMF as an image.  Call it as:   displayAvatar($someIDvariable);
// Replace $someIDvariable with the proper ID_MEMBER variable call.
function displayAvatar($ID_MEMBER) {
global $db, $db_prefix, $modSettings, $boarddir, $boardurl;
 
  $sql = "SELECT u.ID_MEMBER, u.avatar, a.ID_ATTACH, a.file_hash
               FROM {$db_prefix}attachments a, {$db_prefix}members u
               WHERE u.ID_MEMBER=$ID_MEMBER";
  $result = $db->sql_query($sql);
  $row = array();
  $row = $db->sql_fetchrow($result);
  if(!empty($result)) mysql_free_result($result);
  $row['ID_MEMBER'] = intval($row['ID_MEMBER']);
  $avatar = $row['avatar'];
  $ID_ATTACH = intval($row['ID_ATTACH']);
  $file_hash = $row['file_hash'];
  if (!empty($modSettings['currentAttachmentUploadDir']))
                {
                 if (!is_array($modSettings['attachmentUploadDir']))
                $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
                $path = $modSettings['attachmentUploadDir'][$dir];
                } else {
                $path = $modSettings['attachmentUploadDir'];
                }
                $URLpath = str_replace($boarddir, $boardurl, $path);
      $avatarIMG = "";
      if(empty($avatar)){
      // if avatar is uploaded, $avatar field will be empty, so display this:
      $avatarIMG = '<img src="'.$URLpath.'/' . $ID_ATTACH . '_' . $file_hash.'" alt="" />';
      if(empty($ID_ATTACH)) {
      // if both $avatar and $ID_ATTACH are empty, show the blank.gif image
      $avatarIMG = '<img src="'.$boardurl.'/avatars/blank.gif" alt="no avatar" />';
      }
      } elseif (preg_match('#http://#', $avatar)) {
      // if avatar is externally linked, the URL will be in the $avatar field, so display it this way:
      $avatarIMG = '<img src="'.$avatar.'" alt="" />';
      } else {
      // if the user has chosen a file from the avatars directory, display it
      $avatarIMG = '<img src="'.$boardurl.'/avatars/'.$avatar.'" alt="" />';
      }
      echo $avatarIMG;
}


The code below I pulled from PHP Nuke's Journal module...  It will display the user's avatar after doing some LEFT JOIN's of the tables needed.  To pull all the info for the avatar location you need the members and attachments tables.
global $prefix, $db, $module_name, $db_prefix, $modSettings, $boarddir, $boardurl;

$sql = "SELECT *
                  FROM {$prefix}_journal_stats j 
                  LEFT OUTER JOIN {$db_prefix}members u ON u.ID_MEMBER=j.joid
                  LEFT OUTER JOIN {$db_prefix}attachments a ON u.ID_MEMBER=a.ID_MEMBER
                  GROUP BY u.realName ASC
                  ";

        $result = $db->sql_query($sql);
        $row = array();

        while ($row = $db->sql_fetchrow($result)) {

            $row['id'] = intval($row['id']);

            $row['joid'] = intval($row['joid']);

            $row['nop'] = check_html($row['nop'], 'nohtml');

            $row['ldp'] = check_html($row['ldp'], 'nohtml');

            $row['ltp'] = check_html($row['ltp'], 'nohtml');

            $row['micro'] = check_html($row['micro'], 'nohtml');

            $row['ID_MEMBER'] = intval($row['ID_MEMBER']);
            $row['realName'] = check_html($row['realName'], 'nohtml');

            $avatar = check_html($row['avatar'], 'nohtml');
            $ID_ATTACH = intval($row['ID_ATTACH']);
            $file_hash = $row['file_hash'];
            if (!empty($modSettings['currentAttachmentUploadDir']))
                {
                 if (!is_array($modSettings['attachmentUploadDir']))
                $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
                $path = $modSettings['attachmentUploadDir'][$dir];
                } else {
                $path = $modSettings['attachmentUploadDir'];
                }
                $URLpath = str_replace($boarddir, $boardurl, $path);
      $avatarIMG = "";
      if(empty($avatar)){
      // if avatar is uploaded, $avatar field will be empty, so display this:
      $avatarIMG = '<img src="'.$URLpath.'/' . $ID_ATTACH . '_' . $file_hash.'" alt="" />';
      if(empty($ID_ATTACH)) {
      // if both $avatar and $ID_ATTACH are empty, show the blank.gif image
      $avatarIMG = '<img src="'.$boardurl.'/avatars/blank.gif" alt="no avatar" />';
      }
      } elseif (preg_match('#http://#', $avatar)) {
      // if avatar is externally linked, the URL will be in the $avatar field, so display it this way:
      $avatarIMG = '<img src="'.$avatar.'" alt="" />';
      } else {
      // if the user has chosen a file from the avatars directory, display it
      $avatarIMG = '<img src="'.$boardurl.'/avatars/'.$avatar.'" alt="" />';
      }

The avatar image can then be displayed by calling it like this:
echo $avatarIMG;

In SMF, the member can choose their avatar from a selection of stock avatars, from an externally linked avatar, or from an uploaded avatar.  The stock avatars and externally linked avatars can be found from the avatar field of the members table.  If one is uploaded, the ID_ATTACH and file_name fields from the attachments table are needed.

I know the code and variables differ than what is used in SMF, but maybe you can use the code here to figure out what will work for your specific need.

MentaL

@Kays: Using SMF 1.1.10

@spottedhog: I'll try and see if I can make due with that piece of code. :)

spottedhog

No problem.  I am not the heavy coder Kays is, so I tend to be simplistic and use the database tables/fields instead of weeding through the extensive SMF code.  Just giving you an alternative...  :)

Kays

I'm not a heavy coder. I just post a lot of code. :P

Anyways, since it's not recomended to alter the SSI functions. Copy and paste the ssi_boardNews function and rename it. Creating a new function

In this new function find:


global $func;


And replace it with:


global $func, $memberContext;


Then find:


// Find the post ids.
$request = db_query("
SELECT ID_FIRST_MSG
FROM {$db_prefix}topics
WHERE ID_BOARD = $board
ORDER BY ID_FIRST_MSG DESC
LIMIT $start, $limit", __FILE__, __LINE__);
$posts = array();
while ($row = mysql_fetch_assoc($request))
$posts[] = $row['ID_FIRST_MSG'];
mysql_free_result($request);

if (empty($posts))
return array();


And replace it with:


// Find the post ids.
$request = db_query("
SELECT ID_FIRST_MSG, ID_MEMBER_STARTED
FROM {$db_prefix}topics
WHERE ID_BOARD = $board
ORDER BY ID_FIRST_MSG DESC
LIMIT $start, $limit", __FILE__, __LINE__);

$posts = array();
$posters = array();
while ($row = mysql_fetch_assoc($request))
{
$posts[] = $row['ID_FIRST_MSG'];
$posters[] = $row['ID_MEMBER_STARTED'];
}
mysql_free_result($request);

if (empty($posts))
return array();

$posters = array_unique($posters);
loadMemberData($posters);


Find:

$return[] = array(
'message_id' => $row['ID_MSG'],


replace with:


loadMemberContext($row['ID_MEMBER']);

$return[] = array(
'poster' => $memberContext[$row['ID_MEMBER']],
'message_id' => $row['ID_MSG'],


finally, remove the following:


'poster' => array(
'id' => $row['ID_MEMBER'],
'name' => $row['posterName'],
'href' => !empty($row['ID_MEMBER']) ? $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] : '',
'link' => !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName']
),


Then in the output below you can use $news['poster']['avatar']['image'] to display the poster's avatar. You can also now show most of the info which is avaiable in a member's profile.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

MentaL

That's very awesome Kays! Thanks a lot for that. :)

Kays

Well it's not quite the same as for ssi_recentTopics.  But it is a better method to use for ssi_boardNews.

If this serves your needs, could you please mark the topic as being solved by clicking on the green "Mark Topic Solved" button below. :)

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Advertisement: