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
So you mean you don't want it to show anything if there are no people on line ?
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.
It appears to me that there is not a variable called $context['num_users_online']
I'm using SMF 1 what are you using ?
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.
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.
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 ?
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'];
Haha, exactly what I was doing... no problemo... O0
Wow! Excellent explanation. Just learned something new!
Thanx for this. It works ;)
I think my code needs a clean up...
It looks like this:
Please welcome JC357, our newest member.
73076 Posts in 6996 Topics by 1655 Members Array members online: 1
The code is showing the word "Array" and i cant figure it out.
echo'
<table cellspacing="0" cellpadding="0" border="0" align="" width="100%" summary="">
<tbody>
<tr>
<td style="text-align: center;"><span class="smalltext"><span style="color: rgb(127, 127, 127);">';ssi_latestMember(); echo'</span></td>
<td style="text-align: center;"><span class="smalltext"><span style="color: rgb(127, 127, 127);">', $context['common_stats']
['total_posts'], ' ', $txt['posts_made'], ' ', $txt['in'], ' ', $context['common_stats']['total_topics'], ' ', $txt['topics'], ' ', $txt['by'], ' ', $context['common_stats']['total_members'], ' ', $txt['members'], ' ', $who = ssi_whosOnline('array'), ' members online: ', $who['num_users_online'],'</span></td>
</tr>
</tbody>
</table>';
When you start including other SMF variables you'll need to make sure you global them.
Cleaned up a bit for readability too. :)
global $context, $txt;
echo'
<table cellspacing="0" cellpadding="0" border="0" align="" width="100%" summary="">
<tbody>
<tr>
<td style="text-align: center;">
<span class="smalltext"><span style="color: rgb(127, 127, 127);">', ssi_latestMember() ,'</span>
</td>
<td style="text-align: center;">
<span class="smalltext" style="color: rgb(127, 127, 127);">
', $context['common_stats']['total_posts'], ' ', $txt['posts_made'], ' ', $txt['in'], ' ', $context['common_stats']['total_topics'], ' ', $txt['topics'], ' ', $txt['by'], ' ', $context['common_stats']['total_members'], ' ', $txt['members'];
$who = ssi_whosOnline('array');
echo 'members online: ', $who['num_users_online'],'
</span>
</td>
</tr>
</tbody>
</table>';
Excellent. I never thought of the globals. Thank you, it works. ;)
See it on the header area: http://www.chevyavalancheclub.com/index.php :smitten:
The code i had before only showed everything but the user count. Now looks cooler O0