0

One of my sites frequently has more than 1000 concurrent visitors and just for consistency I want to add a thousands separator to the display so it shows like 1,000.

My initial thought was just to add number_format before the variable holding the guest count but this stops the counter working for some reason.

The function in helper.php counting the guests looks like this:

    // show online count
static function getOnlineCount() {
    $db     = JFactory::getDbo();
    // calculate number of guests and users
    $result = array();
    $user_array  = 0;
    $guest_array = 0;
    $query  = $db->getQuery(true);
    $query->select('guest, usertype, client_id');
    $query->from('#__session');
    $query->where('client_id = 0');
    $db->setQuery($query);
    $sessions = (array) $db->loadObjectList();

    if (count($sessions)) {
        foreach ($sessions as $session) {
            // if guest increase guest count by 1
            if ($session->guest == 1 && !$session->usertype) {
                $guest_array ++;
            }
            // if member increase member count by 1
            if ($session->guest == 0) {
                $user_array ++;
            }
        }
    }

    $result['user']  = $user_array;
    $result['guest'] = $guest_array;

    return $result;
}

And in the template the data is displayed using the following:

<?php if ($showmode == 0 || $showmode == 2) : ?>
<?php $guest = JText::plural('MOD_WHOSONLINE_GUESTS', $count['guest']); ?>
<?php $member = JText::plural('MOD_WHOSONLINE_MEMBERS', $count['user']); ?>
<p><?php echo JText::sprintf('MOD_WHOSONLINE_WE_HAVE', $guest, $member); ?></p>

Where do I put the number_format so the separator is added please?

Tobi Frenzen
  • 27
  • 1
  • 5

1 Answers1

0

does this not work?

$guest = JText::plural('MOD_WHOSONLINE_GUESTS',number_format($count['guest'],0,'.',','));
Chris Wesson
  • 299
  • 1
  • 4
  • You are missing one closing bracket there. I've tried it as I hadn't previously added the bits behind ['guest'] Now the number displays but only until it hits 999, then after that it shows "one guest online" – Tobi Frenzen Nov 10 '13 at 13:50
  • I wonder if the issue may partly be with the JText::plural bit. Try this for testing: $guest = number_format($count['guest'],0,'.',','); – Chris Wesson Nov 10 '13 at 14:00
  • I'm fairly certain you wouldn't need "plural" at least since that pluralizes a string, which wouldn't make sense with numbers. – Chris Wesson Nov 10 '13 at 14:01
  • Yes that shows correctly with a comma. I guess I could just leave it like that and hardcode the word guests after it. I'm not so fussed about it working out the plural of guests/guest. But if there's a better solution please let me know :) – Tobi Frenzen Nov 10 '13 at 14:04
  • The plural bit is there I guess because by default it shows the following if one guest is online: "We have one guest and no members online now" / If it's 500 it shows "We have 500 guests and no members online" – Tobi Frenzen Nov 10 '13 at 14:06
  • So the quick-fix to this is to replace it with `code``code` and you could do the same with members, it just won't show 100% correctly when there is only one of each type online. – Tobi Frenzen Nov 10 '13 at 14:14
  • Yep, I see now. JText is looking for an integer and integers don't by default include commas. That's why it was goofing up. It may be a limitation of the JText class. Sorry I couldn't be of more assistance. – Chris Wesson Nov 10 '13 at 14:15
  • That's fine, it's not the perfect solution but this works for me! Many thanks! Why do I need the ,0,'.',',' bit? WHat does it do? In the past I've always happily used number_format($variable) without any problems. – Tobi Frenzen Nov 10 '13 at 14:19
  • Check out this documentation on number_format, it includes a basic run-down of the function and some use cases: http://php.net/manual/en/function.number-format.php – Chris Wesson Nov 10 '13 at 14:39