vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Help with commas in RealChat rooms string (https://vborg.vbsupport.ru/showthread.php?t=66983)

Boofo 07-09-2004 12:32 AM

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. ;)

Velocd 07-09-2004 04:16 AM

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.

Boofo 07-09-2004 04:57 AM

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. ;)

Velocd 07-09-2004 07:00 PM

Replace:
PHP Code:

$result .= $line ', '

With:
PHP Code:

$result .= substr($line0, -1) . ', '

Should do it.

Boofo 07-09-2004 07:45 PM

Excellent! Worked like a charm! Thank you, sir. ;)

Modin 07-09-2004 10:03 PM

you could also use the php trim() function. It removes all surrounding whitespace characters, which is very helpful when dealing with strings.

Boofo 07-09-2004 10:18 PM

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?

Velocd 07-10-2004 04:15 AM

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.

Boofo 07-10-2004 05:50 AM

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?

Boofo 07-10-2004 01:21 PM

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?

Velocd 07-10-2004 03:40 PM

Is that output from the function above? If not, post that code, and I'll take a look at it.

Do refer to the substr function above for truncating strings. A how-to-use reference for it is here:
http://www.php.net/substr

Also, trim:
http://www.php.net/trim

Boofo 07-10-2004 04:07 PM

Quote:

Originally Posted by Velocd
Is that output from the function above? If not, post that code, and I'll take a look at it.

Do refer to the substr function above for truncating strings. A how-to-use reference for it is here:
http://www.php.net/substr

Also, trim:
http://www.php.net/trim

Ok, thanks! ;)

That is from this:

$userCount = getServerAPI( "api.UserCount" );


in my first post at the bottom of the code. It's all part of the same code. The wierd thing is it doesn't do it all of the time. Only about every 5th time or so. Sometimes more, sometimes less.

esology 06-02-2005 11:17 PM

I used these tips as a guideline for my RealChat implementation. I sure appreciate this post. I have a question though, whenever my rooms are empty this is what is diplayed.

ERROR: Room empty.
ERROR: Room empty.

Any ideas.


All times are GMT. The time now is 11:36 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01131 seconds
  • Memory Usage 1,781KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (9)bbcode_php_printable
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (13)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete