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: 447
  • Total: 447

Total users online count, no guests

Started by bluedevil, April 07, 2010, 07:39:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mick

Im trying to make this code to show the total of users currently online. (no buddies and guests)

ie.  xx users 

It should look like "4 users" or "43 users" depending on how many currently online.   

The problem i found is,.... it shows "0 users" and i cannot figure it out.

// "Users online"
echo'
', $context['show_who'] ? '<a href="' . $scripturl . '?action=who">' : '', comma_format($context['num_users_online']), ' ', $context['num_users_online'] == 1 ? $txt['user'] : $txt['users'];


Any ideas? What am i missing?  :P

Freddy

So you mean you don't want it to show anything if there are no people on line ?

Mick

Quote from: freddy888 on April 07, 2010, 07:49:16 PM
So you mean you don't want it to show anything if there are no people on line ?

I would think that if there no one online, it would show "0 users" right?

I keep testing it in a php block but it shows "0 users" even tho i have users online.

Freddy

It appears to me that there is not a variable called $context['num_users_online']

I'm using SMF 1 what are you using ?

Mick

Quote from: freddy888 on April 07, 2010, 08:10:28 PM
It appears to me that there is not a variable called $context['num_users_online']

I'm using SMF 1 what are you using ?

Im using RC3.   I got the code form the board.Index.template.php and stipped the unwanted strings like guests, etc.

Freddy

Hmm...

I tried echoing out all the globals and there is no variable of that name available at the home page level.  It is however available within the forum.

I found the code you refer to.

I guess you need to write some code to count the number of members separately to what SMF is doing.  Maybe someone else has a suggestion for grabbing it.


Freddy

Give me a little while...I think I found something we can use in SSI...so you just want it to show members  and not guests ?

IchBin

#7
Sorry Freddy, I had already started before I saw you reply.

You have to remember that when you pull things out of a template, that variable (or array variabe in this case), may not exists outside of the scope of that page. If you want to get the number of users online you'll probably need to use the SSI function.

Maybe there should be a little tutorial on this stuff. lol Here's a short one. If you want to see what is available in a variable or funciton, remember to use the print_r() function. This will output all the arrays/variables that are stored. So, for instance we want to find the users online. We know this exists in the ssi_whosOnline() function, but we don't know how to grab just that single part. Most (if not all) of the SSI functions echo the output of the function. However, they were cool enough to put in a parameter to allow us to output things to an array. If you look at the function in the actual file it shows this for the signatur of the whosOnline function.
function ssi_whosOnline($output_method = 'echo')

If you read through the code, you'll see that if the output_method is not equal to 'echo', it will output it to an array. So, here's how we can see what is in that array to find out how we can grab the users online. Create a PHP block and put this in it.

$who = whosOnline('array');
echo '<pre>',print_r($who),'</pre>';


This will print out the array (nicely formatted because of our pre tag). You will now see the array that shows something similar to this:
Array
(
    [users_online] => Array
        (
            [1270669111IchBin] => Array
                (
                    [id] => 1
                    [username] => IchBin
                    [name] => IchBinâ„¢
                    [group] => 1
                    [href] => http://www.ichbin.us/index.php?action=profile;u=1
                    [link] => IchBinâ„¢
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Yahoo!] => Array
                (
                    [id] => 0
                    [username] => Yahoo!
                    [name] => Yahoo!
                    [group] => Spiders
                    [href] =>
                    [link] => Yahoo!
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Google] => Array
                (
                    [id] => 0
                    [username] => Google
                    [name] => Google
                    [group] => Spiders
                    [href] =>
                    [link] => Google
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Baidu] => Array
                (
                    [id] => 0
                    [username] => Baidu
                    [name] => Baidu (2)
                    [group] => Spiders
                    [href] =>
                    [link] => Baidu (2)
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] => 1
                )

        )

    [list_users_online] => Array
        (
            [1270669111IchBin] => IchBinâ„¢
            [0Yahoo!] => Yahoo!
            [0Google] => Google
            [0Baidu] => Baidu (2)
        )

    [online_groups] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [name] => Administrator
                    [color] => #FF0000
                )

        )

    [num_guests] => 5
    [num_spiders] => 4
    [num_buddies] => 0
    [num_users_hidden] => 0
    [num_users_online] => 1
    [users] => Array
        (
            [1270669111IchBin] => Array
                (
                    [id] => 1
                    [username] => IchBin
                    [name] => IchBinâ„¢
                    [group] => 1
                    [href] => http://www.ichbin.us/index.php?action=profile;u=1
                    [link] => IchBinâ„¢
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Yahoo!] => Array
                (
                    [id] => 0
                    [username] => Yahoo!
                    [name] => Yahoo!
                    [group] => Spiders
                    [href] =>
                    [link] => Yahoo!
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Google] => Array
                (
                    [id] => 0
                    [username] => Google
                    [name] => Google
                    [group] => Spiders
                    [href] =>
                    [link] => Google
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] =>
                )

            [0Baidu] => Array
                (
                    [id] => 0
                    [username] => Baidu
                    [name] => Baidu (2)
                    [group] => Spiders
                    [href] =>
                    [link] => Baidu (2)
                    [is_buddy] =>
                    [hidden] =>
                    [is_last] => 1
                )

        )

    [guests] => 5
    [hidden] => 0
    [buddies] => 0
    [num_users] => 1
    [total_users] => 10
)


As you can see there is a ton of information there. :) However, you will notice it is organized in some what of a tree fashion. You can pick off any of these variables by simply echoing it out. To get the users online all you have to do is this:
$who = ssi_whosOnline('array');
echo 'Users Online: ', $who['num_users_online'];


Freddy

Haha, exactly what I was doing... no problemo... O0

Mick

Wow!   Excellent explanation.   Just learned something new!

Thanx for this.  It works ;)

This website is proudly hosted on Crocweb Cloud Website Hosting.