Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-14-2009, 03:33 PM   PM User | #1
[vengeance]
Regular Coder

 
Join Date: Aug 2009
Posts: 131
Thanks: 28
Thanked 7 Times in 7 Posts
[vengeance] is an unknown quantity at this point
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:

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

Quote:
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 07:53 PM..
[vengeance] is offline   Reply With Quote
Old 09-14-2009, 05:25 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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?
__________________
Fumigator is offline   Reply With Quote
Old 09-14-2009, 05:38 PM   PM User | #3
[vengeance]
Regular Coder

 
Join Date: Aug 2009
Posts: 131
Thanks: 28
Thanked 7 Times in 7 Posts
[vengeance] is an unknown quantity at this point
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.
[vengeance] is offline   Reply With Quote
Old 09-14-2009, 05:44 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Old 09-14-2009, 05:47 PM   PM User | #5
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
How do you store the list of users online? Are they in an array, do you just have a string?
bacterozoid is offline   Reply With Quote
Old 09-14-2009, 06:05 PM   PM User | #6
[vengeance]
Regular Coder

 
Join Date: Aug 2009
Posts: 131
Thanks: 28
Thanked 7 Times in 7 Posts
[vengeance] is an unknown quantity at this point
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
}

[vengeance] is offline   Reply With Quote
Old 09-14-2009, 07:03 PM   PM User | #7
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
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 07:10 PM..
bacterozoid is offline   Reply With Quote
Old 09-14-2009, 07:38 PM   PM User | #8
[vengeance]
Regular Coder

 
Join Date: Aug 2009
Posts: 131
Thanks: 28
Thanked 7 Times in 7 Posts
[vengeance] is an unknown quantity at this point
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:

Quote:
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.';
    } 
[vengeance] is offline   Reply With Quote
Old 09-14-2009, 07:45 PM   PM User | #9
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
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.';
    }

?>
bacterozoid is offline   Reply With Quote
Users who have thanked bacterozoid for this post:
[vengeance] (09-14-2009)
Old 09-14-2009, 07:53 PM   PM User | #10
[vengeance]
Regular Coder

 
Join Date: Aug 2009
Posts: 131
Thanks: 28
Thanked 7 Times in 7 Posts
[vengeance] is an unknown quantity at this point
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.
[vengeance] is offline   Reply With Quote
Old 09-14-2009, 07:55 PM   PM User | #11
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
Let me know if you have any questions with it. I tried to comment, but some things may be a bit cryptic.
bacterozoid is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:55 AM.


Advertisement
Log in to turn off these ads.