Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts

    Displaying online users

    Hey.

    I want to create a simple script, that counts if there's less than/more than 5 online and display it.

    If 5 or less:

    Name1, name2, name3, name4 and name5 are online.
    If more than 5:

    Name1, name2, name3, name4, name5 and X more people are online.
    The main issue here is that, I don't know how to detect the LAST name, if there's 5 or less users online.

    Like, if there's 3 - Name 1, name2 and name3 are online.
    Last edited by [vengeance]; 09-14-2009 at 08:53 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is your question you don't know how to determine, while you're creating the html to display the names, if you are on the last name and therefore display "and" instead of a comma?

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Fumigator View Post
    Is your question you don't know how to determine, while you're creating the html to display the names, if you are on the last name and therefore display "and" instead of a comma?
    Yeah, that's basically what I'm on a lost track on - simply checking which name is the last and then place "and" in between.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I would just add an "if" statement that checks the current iteration's counter against the number of iterations there are and if they are the same then echo "and" instead of ",". There may be a more clever way of doing it but this works.

  • #5
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    How do you store the list of users online? Are they in an array, do you just have a string?

  • #6
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by bacterozoid View Post
    How do you store the list of users online? Are they in an array, do you just have a string?
    Currently I just do

    PHP Code:
    $getOnline mysql_query("bla bla get online stuff") or die(mysql_error());
    if(
    mysql_num_rows($getOnline) > 0){
    while(
    $online mysql_fetch_array($getOnline)){
    Online users here
    }


  • #7
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    This should work:

    PHP Code:
    <?php

        
    // Get your list of online users
        
    $getOnline mysql_query("bla bla get online stuff") or die(mysql_error());

        
    // Constant
        
    $NUMBER_NAMES_TO_SHOW 5;

        
    // Get the number of rows
        
    $usersOnline mysql_num_rows($getOnline);

        
    /*
         * Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
         * users online, whichever is smaller.
         */
        
    for($i 0$i min($NUMBER_NAMES_TO_SHOW$usersOnline); $i++) {

            
    // Fetch the user from your results
            
    $user mysql_fetch_array($getOnlineMYSQL_ASSOC);
            
            
    // Print out their name
            
    echo $user['name'];

            
    // Determine which delimiter to print out
            
    if(($i == $usersOnline-|| $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
                echo 
    ' and ';
            } else {
                echo 
    ', ';
            }

        }

        
    // If there are more users online
        
    if($usersOnline $NUMBER_NAMES_TO_SHOW) {
            
    $otherUsersOnline $usersOnline $NUMBER_NAMES_TO_SHOW;
            if(
    $otherUsersOnline == 1) {
               echo 
    ' and 1 other user is online.';
            }
            else {
               echo 
    ' and ' $otherUsersOnline ' other users are online.';
            }
        }
        
    // If there are no more users online
        
    else {
            echo 
    ' are online.';
        }

    ?>
    Last edited by bacterozoid; 09-14-2009 at 08:10 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    bacterozoid,

    Thanks for the script. However there's a small problem. It displays almost everything correctly - the comma and the "and" works - but it adds a comma after the last username every time.

    Example:

    Bill, Joe, Ted and Mary, are online.

    Bill, Joe, Ted, Mary and Tom, are online.

    Bill, Joe, Ted, Mary, Tom, and 2 others are online.
    PHP Code:
        // Get your list of online users
        
    $getOnline mysql_query("SELECT `id`,`username` FROM `users` WHERE `status` = 'online' ORDER BY `username` ASC") or die(mysql_error());

        
    // Constant
        
    $NUMBER_NAMES_TO_SHOW 5;

        
    // Get the number of rows
        
    $usersOnline mysql_num_rows($getOnline);

        
    /*
         * Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
         * users online, whichever is smaller.
         */
        
    for($i 0$i min($NUMBER_NAMES_TO_SHOW$usersOnline); $i++) {

            
    // Fetch the user from your results
            
    $online mysql_fetch_array($getOnlineMYSQL_ASSOC);
            
            
    // Print out their name
            
    echo $online['username'];

            
    // Determine which delimiter to print out
            
    if(($i == $usersOnline-|| $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
                echo 
    ' and ';
            } else {
                echo 
    ', ';
            }

        }

        
    // If there are more users online
        
    if($usersOnline $NUMBER_NAMES_TO_SHOW) {
            
    $otherUsersOnline $usersOnline $NUMBER_NAMES_TO_SHOW;
            if(
    $otherUsersOnline == 1) {
               echo 
    ' and 1 other user is online.';
            }
            else {
               echo 
    ' and ' $otherUsersOnline ' other users are online.';
            }
        }
        
    // If there are no more users online
        
    else {
            echo 
    ' are online.';
        } 

  • #9
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Fixed

    PHP Code:
    <?php

        
    // Get your list of online users
        
    $getOnline mysql_query("bla bla get online stuff") or die(mysql_error());

        
    // Constant
        
    $NUMBER_NAMES_TO_SHOW 5;

        
    // Get the number of rows
        
    $usersOnline mysql_num_rows($getOnline);

        
    /*
         * Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
         * users online, whichever is smaller.
         */

        
    $iterations min($NUMBER_NAMES_TO_SHOW$usersOnline);
        for(
    $i 0$i $iterations$i++) {

            
    // Fetch the user from your results
            
    $user mysql_fetch_array($getOnlineMYSQL_ASSOC);
            
            
    // Print out their name
            
    echo $user['name'];

            
    // Determine which delimiter to print out
            
    if(($i == $usersOnline-|| $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
                echo 
    ' and ';
            } else if(
    $i $iterations-1) {
                echo 
    ', ';
            }

        }

        
    // If there are more users online
        
    if($usersOnline $NUMBER_NAMES_TO_SHOW) {
            
    $otherUsersOnline $usersOnline $NUMBER_NAMES_TO_SHOW;
            if(
    $otherUsersOnline == 1) {
               echo 
    ' and 1 other user is online.';
            }
            else {
               echo 
    ' and ' $otherUsersOnline ' other users are online.';
            }
        }
        
    // If there are no more users online
        
    else {
            echo 
    ' are online.';
        }

    ?>

  • Users who have thanked bacterozoid for this post:

    [vengeance] (09-14-2009)

  • #10
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Thanks a lot bacterozoid - works flawlessly! Just what I was looking for.

    I'm going to take a deeper look into the code and learn from it too.

  • #11
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Let me know if you have any questions with it. I tried to comment, but some things may be a bit cryptic.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •