Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2004, 12:32 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Help with commas in RealChat rooms string

Could anyone please help me add commas to the string of RealChat rooms that the following code generates?

PHP Code:
// REALCHAT WHO'S ONLINE
$port 10010;
function 
getServerAPI$apiCommand ) {
global 
$port;
$result "";
$fp fsockopen("localhost"$port, &$errno, &$errstr2);
if(!
$fp) {
echo 
"$errstr ($errno)\n";
} else {
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");
$header true;
while(!
feof($fp)) {
$line fgets($fp,128);
if ( 
$header == false $result .= $line;
if ( 
trim($line) == "" $header false;
}
fclose($fp);
}
return 
$result;
}
$userCount getServerAPI"api.UserCount" );
$roomList getServerAPI"api.RoomList" );
// End REALCHAT WHO'S ONLINE 
Right now it looks like this:

Quote:
Available RealChat Rooms: The Bear's Den The Lobby Fun Staff only! No topic Bear's Den
The rooms are:

The Bear's Den
The Lobby
Fun
Staff only!
No topic
Bear's Den

I would like commas after the names except for the last one.

This is the code that came with it so I have no idea how to add the commas.

Thanks in advance.
Reply With Quote
  #2  
Old 07-09-2004, 04:16 AM
Velocd's Avatar
Velocd Velocd is offline
 
Join Date: Mar 2002
Location: CA University
Posts: 1,696
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Assuming that each channel is being retrieved on its own line, you should be able to:

PHP Code:
function getServerAPI($apiCommand)
{
    global 
$port;
    
    
$result "";
    
    
$fp fsockopen("localhost"$port, &$errno, &$errstr2);
    
    if(!
$fp)
    {
        echo 
"$errstr ($errno)\n";
    }
    else 
    {
        
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");
        
        
$header true;

        while(!
feof($fp))
        {
            
$line fgets($fp,128);
            
            
// print $line;

            
if ($header == false)
                
$result .= $line ', ';

            if (
trim($line) == "")
                
$header false;
        }

        
fclose($fp);
    }
    
    return 
substr($result0, -2); // trim the last ', '

To get the comma separated list. If it's not separated by a line, uncomment the echo I have in the loop and check the output.
Reply With Quote
  #3  
Old 07-09-2004, 04:57 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Velocd
Assuming that each channel is being retrieved on its own line, you should be able to:

PHP Code:
function getServerAPI($apiCommand)
{
global 
$port;
 
$result "";
 
$fp fsockopen("localhost"$port, &$errno, &$errstr2);
 
if(!
$fp)
{
echo 
"$errstr ($errno)\n";
}
else 
{
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");
 
$header true;
 
while(!
feof($fp))
{
$line fgets($fp,128);
 
// print $line;
 
if ($header == false)
$result .= $line ', ';
 
if (
trim($line) == "")
$header false;
}
 
fclose($fp);
}
 
return 
substr($result0, -2); // trim the last ', '

To get the comma separated list. If it's not separated by a line, uncomment the echo I have in the loop and check the output.
Ok that almost works. We have an extra space between the room name and the commas, though. Like this:

Quote:
Available RealChat Rooms: The Bear's Den , The Lobby , Fun , Staff only! , No topic
How do I take the extra space out of that?

And thank you, very much, sir.
Reply With Quote
  #4  
Old 07-09-2004, 07:00 PM
Velocd's Avatar
Velocd Velocd is offline
 
Join Date: Mar 2002
Location: CA University
Posts: 1,696
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Replace:
PHP Code:
$result .= $line ', '
With:
PHP Code:
$result .= substr($line0, -1) . ', '
Should do it.
Reply With Quote
  #5  
Old 07-09-2004, 07:45 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent! Worked like a charm! Thank you, sir.
Reply With Quote
  #6  
Old 07-09-2004, 10:03 PM
Modin Modin is offline
 
Join Date: Jun 2004
Posts: 162
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

you could also use the php trim() function. It removes all surrounding whitespace characters, which is very helpful when dealing with strings.
Reply With Quote
  #7  
Old 07-09-2004, 10:18 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Modin
you could also use the php trim() function. It removes all surrounding whitespace characters, which is very helpful when dealing with strings.
How would you incorporate that into this?
Reply With Quote
  #8  
Old 07-10-2004, 04:15 AM
Velocd's Avatar
Velocd Velocd is offline
 
Join Date: Mar 2002
Location: CA University
Posts: 1,696
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Instead of doing:

PHP Code:
$result .= substr($line0, -1) . ', '
Use:

PHP Code:
$result .= trim($line) . ', '
Probably isn't any faster, but it's more appropriate.
Reply With Quote
  #9  
Old 07-10-2004, 05:50 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Velocd
Instead of doing:

PHP Code:
$result .= substr($line0, -1) . ', '
Use:

PHP Code:
$result .= trim($line) . ', '
Probably isn't any faster, but it's more appropriate.
Thank you, again, sir.

For future reference, will the trim get rid of whitespace on both sides of the line or just the preceding whitespace?
Reply With Quote
  #10  
Old 07-10-2004, 01:21 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I am getting a comma after the amount of users in the chat room randonly now. It doesn't happen all the time but every once in a while. Like this:

Quote:
RealChat Users Connected: 0,
Is there any way to prevent this?
Reply With Quote
Reply

Thread Tools
Display Modes

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. The time now is 06:51 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04156 seconds
  • Memory Usage 2,288KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (9)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete