View Full Version : Adding commas question
Boofo
09-22-2005, 10:08 AM
Can someone please tell me how I would add commas after the room names and user names in this piece of code. I don't want and comma after the last room name or lat user name, though, if possible.
$port = 10010;
function getServerAPI( $apiCommand )
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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;
}
$userList = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
Andreas
09-22-2005, 10:28 AM
I don't see any User- or Room Names in this Code ...
Or in other Words:
How does $userList look like?
Boofo
09-22-2005, 11:54 AM
Here is the code I am currently using, but as you can see there is an extra comma after the room names. It happens with more than one user, too. That's why I thought with fresh code as above, it might be easier to do the commas right. ;)
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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)
if($apiCommand!='api.UserList') {
$result .= trim($line) . ', ';
}else {
$result .= substr($line, 0, -1); // trim the last ', '
}
if (trim($line) == "")
$header = false;
}
fclose($fp);
}
return substr($result, 0, -2); // trim the last ', '
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
This is what it looks like after we get the roomlist and the userlist. If you need to see the template, let me know. ;)
Andreas
09-22-2005, 11:56 AM
OK, so where do you want to add commas (as there already seem to be some)?
Boofo
09-22-2005, 11:57 AM
I edited my post above. ;)
Andreas
09-22-2005, 12:10 PM
I don't really understand the Code :)
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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);
echo("Raw Data: '$line'");
if ($header == false)
{
$result .= trim($line) . ', ';
}
if (trim($line) == "")
{
$header = false;
}
}
fclose($fp);
}
return substr($result, 0, -2); // trim the last ', '
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
What's the Raw Data?
Boofo
09-22-2005, 12:32 PM
It's for RealChat. The roomList is for the rooms and the userList is for the users. That's about all I know about it.
Here is the raw data.
Raw Data: 'HTTP/1.1 200 OK 'Raw Data: 'Date: Thu Sep 22 09:30:01 EDT 2005 'Raw Data: 'Server: RealChat Server Version 2.2.2d, http control server 'Raw Data: 'Cache-Control: no-cache, must-revalidate 'Raw Data: 'Expires: Mon, 26 Jul 1997 05:00:00 GMT 'Raw Data: 'Pragma: no-cache 'Raw Data: 'Connection: close 'Raw Data: 'Content-Type: text/html; charset=iso-8859-1 'Raw Data: ' 'Raw Data: ' 'Raw Data: ' 'Raw Data: 'HTTP/1.1 200 OK 'Raw Data: 'Date: Thu Sep 22 09:30:01 EDT 2005 'Raw Data: 'Server: RealChat Server Version 2.2.2d, http control server 'Raw Data: 'Cache-Control: no-cache, must-revalidate 'Raw Data: 'Expires: Mon, 26 Jul 1997 05:00:00 GMT 'Raw Data: 'Pragma: no-cache 'Raw Data: 'Connection: close 'Raw Data: 'Content-Type: text/html; charset=iso-8859-1 'Raw Data: ' 'Raw Data: 'The Bear's Den 'Raw Data: 'The Lobby 'Raw Data: 'Executive Den 'Raw Data: ''
Marco van Herwaarden
09-22-2005, 12:34 PM
Simplest solution in general to a problem like this is to put all usernames/roomnames into an array. Then when all names are collected do:
$usersstring = explode(", ", $usersarray);
Boofo
09-22-2005, 12:41 PM
And how would you go about putting them all into an array?
Andreas
09-22-2005, 12:44 PM
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
if (!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");
$header = true;
while(!feof($fp))
{
$line = trim(fgets($fp,128));
if ($header == false AND $line != '')
{
$result .= "$line, ";
}
else if ($line == '')
{
$header = false;
}
}
fclose($fp);
}
return substr($result, 0, -2); // trim the last ', '
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
Should work.
Marco van Herwaarden
09-22-2005, 12:52 PM
How does $roomList and $userCount look like now?
Boofo
09-22-2005, 12:58 PM
Ok, after I fixed the line:
else if ($line) == '')
It worked for the rooms. But the users shows a comma after the name now (like in the attched pic).
Here is the code I am using that you sent me after the line fix:
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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 AND ($line = trim($line)) != '')
{
$result .= "$line, ";
}
else if (trim($line) == '')
{
$header = false;
}
}
fclose($fp);
}
return substr($result, 0, -2); // trim the last ', '
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
Marco van Herwaarden
09-22-2005, 02:01 PM
Try:
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = array();
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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 AND ($line = trim($line)) != '')
{
$result{} = trim($line);
}
else if (trim($line) == '')
{
$header = false;
}
}
fclose($fp);
}
return implode(", ", $result);
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
Boofo
09-22-2005, 02:18 PM
Got this error with that code:
Parse error: parse error, unexpected '}' in /xxxx/xxxx/xxxxxx_xxxx/xxxxxxx/index.php(460) : eval()'d code on line 20
Marco van Herwaarden
09-22-2005, 02:27 PM
$result{} = trim($line);
Should be:
$result[] = trim($line);
Boofo
09-22-2005, 02:35 PM
Well, no error, but it still adds a comma after the user name like so:
RealChat Users Connected: The Bear's Den [1] Boofo,
The room names look fine. ;)
Marco van Herwaarden
09-22-2005, 02:55 PM
Please add the following line before the line with the return statement, and post the output here:
echo "<br>result: <pre>";print_r($result);echo "</pre>";
Boofo
09-22-2005, 03:08 PM
Here you go. ;)
result:
Array
(
)
result:
Array
(
[0] => The Bear's Den
[1] => The Lobby
[2] => Executive Den
)
Marco van Herwaarden
09-22-2005, 05:30 PM
Well the $userCount seems to be empty, so i don't get where:
The Bear's Den [1] Booby, get's filled. Don't you have this as static text in your template maybe, including the comma?
Boofo
09-22-2005, 05:36 PM
Nope, this is what I have in the template for that line:
<div>$vbphrase[bh_realchat_users]: $userCount</div>
Marco van Herwaarden
09-22-2005, 07:56 PM
Then from where comes that text? Or didn't you show the whole script?
Boofo
09-22-2005, 10:12 PM
api.UserList and api.RoomList are how the info is pulled out. That's all I know about tthis as I got it from the ReahChat site in their documentation. And that is the full script.
were you in the room when you did the last test marco asked for?
Boofo
09-23-2005, 12:43 AM
Yes, sir, I was. ;)
Marco van Herwaarden
09-23-2005, 11:24 AM
Well if the array is empty, then (if not using mroe code) there is no way the roomname and your membername are in $userCount.
Also you say this is the complete script, but i don't see a Template evaluated or outputed anywhere. So i guess this can't be all.
Boofo
09-23-2005, 11:36 AM
Ok, here is the COMPLETE code I am using in the forumhome_complete hook right now:
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = array();
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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 AND ($line = trim($line)) != '')
{
$result[] = trim($line);
}
else if (trim($line) == '')
{
$header = false;
}
}
fclose($fp);
}
return implode(", ", $result);
}
$userList = getServerAPI( "api.UserList" );
$userCount = getServerAPI( "api.UserCount" );
$roomList = getServerAPI( "api.RoomList" );
eval('$forumhomerealchat = "' . fetch_template('forumhome_realchat') . '";');
and here is the template for that code:
<tbody>
<tr>
<td class="thead" colspan="2">
<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activechatters')"><img id="collapseimg_forumhome_activechatters" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
<a href="javascript:showwin('$vboptions[bburl]/chat/chat.php', 'width=700,height=600, top=50, left=50')" style="text-decoration: none;">$vbphrase[bh_realchat_who]</a>: <if condition="$userCount == '0'">No Users<else /><if condition="$userCount == '1'">$userCount User<else />$userCount Users</if></if>
</td>
</tr>
</tbody>
<tbody id="collapseobj_forumhome_activechatters" style="$vbcollapse[collapseobj_forumhome_activechatters]">
<tr>
<td class="alt2">
<a href="javascript:showwin('$vboptions[bburl]/chat/chat.php', 'width=700,height=600, top=50, left=50')"><img class="inlineimg" src="$stylevar[imgdir_misc]/chat.gif" alt="$vboptions[bburl] $vbphrase[bh_realchat_rooms]" border="0" /></a></td>
<td class="alt1" width="100%">
<div class="smallfont">
<div>$vbphrase[bh_realchat_available_rooms]: $roomList</div>
<hr size="1" style="color:$stylevar[tborder_bgcolor]" />
<if condition="$userList == ''">
<div>$vbphrase[bh_realchat_users]: None</div>
<else />
<div>$vbphrase[bh_realchat_users]: $userList</div>
</if>
</div>
</td>
</tr>
</tbody>
and then I add $forumhomerealchat to the forumhome template where I want it to show.
Now, here is the code I WAS using before (in the index.php on vB 3.0). It worked but it showed commas after the last room name at times but not all the time. It was sporadic.
// REALCHAT WHO'S ONLINE
$port = 10010;
function getServerAPI($apiCommand)
{
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
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)
if($apiCommand!='api.UserList') {
$result .= trim($line) . ', ';
}else {
$result .= substr($line, 0, -1); // trim the last ', '
}
if (trim($line) == "")
$header = false;
}
fclose($fp);
}
return substr($result, 0, -2); // trim the last ', '
}
$userCount = getServerAPI( "api.UserList" );
$roomList = getServerAPI( "api.RoomList" );
// End REALCHAT WHO'S ONLINE
Marco van Herwaarden
09-23-2005, 11:58 AM
You are now executing the function 3 times, and you only posted 2 arrays. Please run again with the print of the array values, and while 1 or more people in the chat.
Boofo
09-23-2005, 12:20 PM
You mean run this again?echo "<br>result: <pre>";print_r($result);echo "</pre>";
Marco van Herwaarden
09-23-2005, 12:31 PM
yes..
Boofo
09-23-2005, 01:14 PM
Here you go. I was in the chat rioom when I ran this.
result:
Array
(
[0] => The Bear's Den [1] Boofo,
)
result:
Array
(
[0] => 1
)
result:
Array
(
[0] => The Bear's Den
[1] => The Lobby
[2] => Executive Den
)
Andreas
09-23-2005, 01:21 PM
So there if comes from ... the comma is already in the raw data.
Eg. seems to be smth. like this
userlist ::= roomname [userinfo].
userinfo ::= " " usercount " " users.
roomname ::= alphanam {alphanum}.
usercount ::= "[" digit {digit} "]".
users ::= user {user}.
user ::= alphanum {alphanum} ",".
alphanum ::= letter | digit | "_" | "-".
Marco van Herwaarden
09-23-2005, 01:22 PM
Ok, so the comma is already there when you get it from the server.
Change:
$result[] = trim($line);
To
$result[] = (substr(trim($line), -1) == "," ? substr(trim($line)0, -1) : trim($line));
Boofo
09-23-2005, 01:39 PM
After changing that line I now get this eror.
Parse error: parse error, unexpected T_LNUMBER in /xxxx/xxxx/xxxxxx_xxxx/xxxxxx/index.php(460) : eval()'d code on line 20
Andreas
09-23-2005, 01:43 PM
Try
$result[] = ((substr(trim($line), -1)) == ',' ? substr(trim($line), 0, -1) : trim($line));
Boofo
09-23-2005, 01:45 PM
The comma is still there. :(
But the error is gone. ;)
RealChat Users Connected: The Bear's Den [1] Boofo,
Andreas
09-23-2005, 01:47 PM
Post edited :D
(35 replies for a freaking comma ... yeah, that's my business :p)
Boofo
09-23-2005, 01:56 PM
ROFLMAO @ Kirby
Bingo!!!
We have a winner!
RealChat Users Connected: The Bear's Den [1] Boofo
Now the comma will come back if we have users in another room that gets added after that, right? LOL
Andreas
09-23-2005, 01:58 PM
Dunno. Test it.
Boofo
09-23-2005, 02:13 PM
I'll test it when I get someone on and let you know. I'm sure it will be fine, though. ;)
Thank you, sirs (Marco and Kirby). ;)
Marco van Herwaarden
09-23-2005, 02:30 PM
It could have been done with a lot less posts if you would have just posted all this info immediate. :D
We really had to pull it out of you.
Marco van Herwaarden
09-23-2005, 02:31 PM
@Booboy
You homepage setting in your profile is against our Rules. Please remove the refferer. :D
Andreas
09-23-2005, 02:32 PM
Roflol.
Boofo
09-23-2005, 02:34 PM
Maybe if you would have told me excatly what you wanted from the beginning, I could have supplied it then. I gave you all the info I had at the time and when you asked for it. ;)
And how is the referrer against any rules? That has been there forever.
Boofo
09-23-2005, 09:01 PM
Latest test results ... working as designed! ;)
Thanks again to the both of you. ;)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.