1 (edited by Peter 2007-03-02 23:36)

Topic: Users online with pictures, newest 5 users with pictures

How can I show the users online with pictures? Has anyone else done that?

I have a picture upload/resize script that I'm going to integrate in PunBB's registration. The thumbnail profile picture link will be based on the username - probably not the way to do it, but it works.

I don't even see the "users online" in my current installation, but that's probably because I have no users yet ;-)

What code produces this?

Online: bobitt, falconflyz, guardian34, Peter

The index.php has this:

if ($pun_config['o_users_online'] == '1')
{
    // Fetch users online info and generate strings for output
    $num_guests = 0;
    $users = array();
    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
            $users[] = "\n\t\t\t\t".'<dd><a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a>';
        else
            ++$num_guests;
    }

    $num_users = count($users);
    echo "\t\t\t\t".'<dd>'. $lang_index['Users online'].': <strong>'.$num_users.'</strong></dd>'."\n\t\t\t\t".'<dd>'.$lang_index['Guests online'].': <strong>'.$num_guests.'</strong></dd>'."\n\t\t\t".'</dl>'."\n";


    if ($num_users > 0)
        echo "\t\t\t".'<dl id="onlinelist" class= "clearb">'."\n\t\t\t\t".'<dt><strong>'.$lang_index['Online'].': </strong></dt>'."\t\t\t\t".implode(',</dd> ', $users).'</dd>'."\n\t\t\t".'</dl>'."\n";
    else
        echo "\t\t\t".'<div class="clearer"></div>'."\n";

}
else
    echo "\t\t".'</dl>'."\n\t\t\t".'<div class="clearer"></div>'."\n";


?>

So that must be it, right? How can I modify or expand this to include picture links or data from other database fields, besides just the username? Any other fun things I could do with this?

Also, is there a way to show the latest 5 or 10 new users, instead of just the one newest registered user?

2

Re: Users online with pictures, newest 5 users with pictures

Anyone?

Re: Users online with pictures, newest 5 users with pictures

    if ($num_users > 0)
        echo "\t\t\t".'<dl id="onlinelist" class= "clearb">'."\n\t\t\t\t".'<dt><strong>'.$lang_index['Online'].': </strong></dt>'."\t\t\t\t".implode(',</dd> ', $users).'</dd>'."\n\t\t\t".'</dl>'."\n";
    else
        echo "\t\t\t".'<div class="clearer"></div>'."\n";

to

    if ($num_users > 0)
    {
        echo "\t\t\t".'<dl id="onlinelist" class= "clearb">'."\n\t\t\t\t".'<dt><strong>'.$lang_index['Online'].': </strong></dt>'."\t\t\t\t"
        foreach ($users as $user)
        {
            echo '<dd><img src="img/mycutomavartars/'.$user.'" />'.$user.'</dd>';
        }
        echo "\n\t\t\t".'</dl>'."\n";
    }
    else
        echo "\t\t\t".'<div class="clearer"></div>'."\n";

Assuming:

The thumbnail profile picture link will be based on the username

If that's not the case you'll have to query the db for each users avatar number.

echo "deadram"; echo; fortune;

4 (edited by Peter 2007-03-06 21:55)

Re: Users online with pictures, newest 5 users with pictures

Thanks deadram! smile

Can't wait to test this! Don't have time now, but will report back when I have.

edit:

Someone posted some excellent code for automatic "avatar" resizing here.

5 (edited by Peter 2007-03-18 05:40)

Re: Users online with pictures, newest 5 users with pictures

I almost have this working with regular avatars, using this code:

    if ($num_members > 0)
    {
        echo "\t\t\t"."\n\t\t\t\t".$lang_index['Online'].': '."\t\t\t\t";
        foreach ($members as $user)
        {
            echo "<p><img src='http://mywebsite.com/common/img/avatars/$id.jpg' /><br />$user</p>";
        }
        echo "\n\t\t\t".'</dl>'."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';

I had to make some edits to prevent syntax errors. Read 'users' where it says 'members'. That's just part of my customization.

This code produces the right names of $users online, but I can't get the right avatar to show up. If I just fill in the number the address works, but how can I get the code "to query the db for each users avatar number"?

BTW, what does the \n\t\t\t stuff do?

edit:

Someone here tried to do the same thing but the solution looks overly complicated and apparently produces an ugly result.

6 (edited by Peter 2007-03-17 23:28)

Re: Users online with pictures, newest 5 users with pictures

if ($num_users > 0)
    {
        echo "<br />Currently online:";
        foreach ($users as $id => $user)
        {
            echo "<p><img src='http://website.com/common/img/avatars/$id.jpg' /><br />$user</p>";
        }
        echo "\n\t\t\t"."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';

This variation puts a 0 at $id and the name of a logged in member at $user.

Still can't figure out how "to query the db for each users avatar number". I've looked into the PHP manual at foreach, but I can't make sense of it. Should I do something with $members = array();?

I've also tried variations of this, but keep getting syntax errors:

    if ($num_members > 0)
    {
        echo "<br />Currently online:";
        foreach ($members as $user)
        {
            echo "<p><img src='http://mywebsite.com/common/img/avatars/$pun_user_online['user_id'].jpg' /><br />$user</p>";
        }
        echo "\n\t\t\t"."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';

Any help very welcome!

7 (edited by Peter 2007-03-18 05:35)

Re: Users online with pictures, newest 5 users with pictures

This code comes close:

    if ($num_users > 0)
    {
        echo "<br />Currently online:";
        
        foreach ($users as $user)
        {
            echo '<p><img src="http://mywebsite.com/common/img/avatars/'.$pun_user['id'].'.jpg" /><br />'.$user.'</p>';

        }
        echo "\n\t\t\t"."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';

It produces the avatar+name from the user that's logged in.

I'm not sure however if it would show avatars from each user if more users are logged in. I've tried to test that with a second laptop, but on both laptops I only see avatar+name of the identity I logged in with on that laptop.

The regular "online" list also gives only one user on each laptop, so there could be another reason. Don't they register as different users online because they are from the same IP address (not sure they are, just guessing)?

Can anyone with some knowledge of PHP confirm if I'm on the right track? Any suggestions, insights very welcome!

Re: Users online with pictures, newest 5 users with pictures

$pun_user['id'] is the ID of the currently logged in user. You'll need to modify your query so that it grabs the user id as well as the username.

9

Re: Users online with pictures, newest 5 users with pictures

Smartys wrote:

$pun_user['id'] is the ID of the currently logged in user. You'll need to modify your query so that it grabs the user id as well as the username.

Thanks, but I don't know PHP. How do I modify my query "so that it grabs the user id as well as the username"?

Re: Users online with pictures, newest 5 users with pictures

Mmm, the code that deadram gave you isn't going to work for you
I'd need to see the code for the page to be sure what's going on and what to edit

11 (edited by Peter 2007-03-18 14:38)

Re: Users online with pictures, newest 5 users with pictures

I now have this in index.php:

<? if ($pun_config['o_users_online'] == '1')
{
    // Fetch users online info and generate strings for output
    $num_guests = 0;
    $users = array();
    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
            $users[] = "\n\t\t\t\t".'<a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a><br />';
        else
            ++$num_guests;
    }

    $num_users = count($users);
    echo "\t\t\t\t".$lang_index['users online'].': '.$num_users."\n\t\t\t\t".$lang_index['Guests online'].': '.$num_guests."\n\t\t\t"."\n".'<br />';


    if ($num_users > 0)
    {
        echo "<br />Currently online:";
        
        foreach ($users as $user)
        {
            echo '<p><img src="http://mywebsite.com/common/img/avatars/'.$pun_user['id'].'.jpg" /><br />'.$user.'</p>';

        }
        echo "\n\t\t\t"."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';


}
else
    echo "\t\t"."\n\t\t\t"."\n".'<br />';

?>

Most of it is untouched PunBB code and works fine. The edits are after foreach ($users as $user).

I understand MySQL has a table 'online' and I have to get the 'user_id' and 'ident' from there. Both are already queried (?) at the top of the code, so there should be a simple way to get them in that echo line, right?

Re: Users online with pictures, newest 5 users with pictures

<?php
if ($pun_config['o_users_online'] == '1')
{
    // Fetch users online info and generate strings for output
    $num_guests = 0;
    $users = array();
    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
            $users[$pun_user_online['user_id']] = "\n\t\t\t\t".'<a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a><br />';
        else
            ++$num_guests;
    }

    $num_users = count($users);
    echo "\t\t\t\t".$lang_index['users online'].': '.$num_users."\n\t\t\t\t".$lang_index['Guests online'].': '.$num_guests."\n\t\t\t"."\n".'<br />';


    if ($num_users > 0)
    {
        echo "<br />Currently online:";
        
        foreach ($users as $user_id => $username)
        {
            echo '<p><img src="http://bizpartnerlounge.com/common/img/avatars/'.$user_id.'.jpg" /><br />'.$username.'</p>';

        }
        echo "\n\t\t\t"."\n";
    }
    else
        echo "\t\t\t"."\n".'<br />';


}
else
    echo "\t\t"."\n\t\t\t"."\n".'<br />';

?>

I believe that should work, keep in mind though that if the user uploads a GIF or PNG instead of a JPEG, it won't show

13

Re: Users online with pictures, newest 5 users with pictures

Thanks! It seems to work. smile

Now going to test it with a second laptop...

The extension is no problem. I use thame^'s resize solution that only produces jpg's (I think...)

Re: Users online with pictures, newest 5 users with pictures

Only when resizing images that are too large: I can upload a properly sized GIF or PNG wink

15

Re: Users online with pictures, newest 5 users with pictures

I've tried adding '.$avatar_format.' instead of jpg, but that didn't work. Is there an easy solution?

Re: Users online with pictures, newest 5 users with pictures

Take a look at how viewtopic.php does it

17 (edited by Peter 2007-03-18 15:54)

Re: Users online with pictures, newest 5 users with pictures

viewtopic.php has this:

        if ($pun_config['o_avatars'] == '1' && $cur_post['use_avatar'] == '1' && $pun_user['show_avatars'] != '0')
        {
            if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.gif'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.gif" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.jpg'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.jpg" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.png'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$cur_post['poster_id'].'.png" '.$img_size[3].' alt="" />';
        }
        else
            $user_avatar = '';

profile.php has similar code. I probably need an if/else thingy, but I don't need the getimagesize stuff and don't know how to worm this into what I had. The possible variations to try run in the millions.

I give up.

18

Re: Users online with pictures, newest 5 users with pictures

did anyone get this working?


Q

My stuff or my style might sux, but atleast I'm willing to help when I can.
Don't be stupid and help ! We are the stupid one's !!!

Re: Users online with pictures, newest 5 users with pictures

I will donate £20 to PunBB if someone can get this working.
I know that's not much but that's all I got

Re: Users online with pictures, newest 5 users with pictures

I'll do it without joining the users table wink

index.php

FIND

    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
            $users[] = "\n\t\t\t\t".'<dd><a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a>';
        else
            ++$num_guests;
    }

REPLACE WITH

    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
        {
            if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.gif'))
                $users[] = "\n\t\t\t\t".'<dd><a title="'.pun_htmlspecialchars($pun_user_online['ident']).'" href="profile.php?id='.$pun_user_online['user_id'].'"><img src="'.$pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.gif" '.$img_size[3].' alt="" /></a>';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.jpg'))
                $users[] = "\n\t\t\t\t".'<dd><a title="'.pun_htmlspecialchars($pun_user_online['ident']).'" href="profile.php?id='.$pun_user_online['user_id'].'"><img src="'.$pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.jpg" '.$img_size[3].' alt="" /></a>';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.png'))
                $users[] = "\n\t\t\t\t".'<dd><a title="'.pun_htmlspecialchars($pun_user_online['ident']).'" href="profile.php?id='.$pun_user_online['user_id'].'"><img src="'.$pun_config['o_avatars_dir'].'/'.$pun_user_online['user_id'].'.png" '.$img_size[3].' alt="" /></a>';
            else
                $users[] = "\n\t\t\t\t".'<dd><a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a>';
        }
        else
            ++$num_guests;
    }

Re: Users online with pictures, newest 5 users with pictures

Hi Smartys.
I wonder.... I tried your code but nothing changed at all.
Would you be willing to edit my files directly if I give you the cpanel user/pass to my hosting account?
And once its working I'll donate to the punbb option?
My users will love this,  know they will

Cheers
Mark

Re: Users online with pictures, newest 5 users with pictures

Sure, just send me an email

Re: Users online with pictures, newest 5 users with pictures

Can I ask what is displayed if the user has not uploaded an avatar?

Is it possible to display a default image for users who haven't uploaded something?

Re: Users online with pictures, newest 5 users with pictures

downliner wrote:

Can I ask what is displayed if the user has not uploaded an avatar?

Is it possible to display a default image for users who haven't uploaded something?

In the example I posted? The username is displayed like normal
You could easily change the else clause to display an image instead

Re: Users online with pictures, newest 5 users with pictures

Big thanks to Smartys for helping with this. It's working now
My $40 donation is in the pipeline