View Full Version : need if conditional....
indie
03-06-2005, 06:43 AM
For this chat script, I need an if conditional so when there are no names to display, it says "No members in Chat".
Also, if you can figure out how to use the usergroup html markup for usernames (ex: like bold names for usergroups that have bold names in online box) that would be great.
---------------------------------------------
function usersinroom( $room = "" )
{
$cms = $GLOBALS['fc_config']['cms'];
$list = array();
if($room) {
$stmt = new Statement("SELECT userid, state, color, lang, roomid FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL AND roomid=?");
$rs = $stmt->process($room);
} else {
$stmt = new Statement("SELECT userid FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL");
$rs = $stmt->process();
}
while($rec = $rs->next()) $list[] = array_merge($cms->getUser($rec['userid']), $rec);
return $list;
}
-------------------
then is says:
-------------------
$users = usersinroom();
$arr = array();
foreach( $users as $user ) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
=========================
thanks
Scrub
03-06-2005, 07:28 AM
Change
$users = usersinroom();
$arr = array();
foreach( $users as $user ) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
To
$users = usersinroom();
$arr = array();
foreach( $users as $user ) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
} else {
echo "<b>No Members in Chat</b>";
}
I belive that should work?
indie
03-06-2005, 07:46 PM
No, this caused the page to be just a white page. Code error somewhere.
Link14716
03-06-2005, 07:58 PM
foreach is a loop, not a conditional. Therefore:
$users = usersinroom();
$arr = array();
if (!empty($users)) {
foreach ($users as $user) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
} else {
echo "<b>No Members in Chat</b>";
}
why-not
03-06-2005, 08:17 PM
Hi
You should really be checking if the query returns any thing, there is no sense doing a while() loop on a null returned result set! Also it would be better to do the link building inside the usersinoom() function and just return the links or if no users are in the room, just return "No Users In Room". It wasted coding to do (2) loops and if()s when you don't have to! Logic is missing from this!!!
Show me or tell me the database class you are using, and I will writre you how to do it the right way! I need to know the DB class to see if it has a function that returns the row count for the query!
Sonia!
indie
03-06-2005, 09:11 PM
Thanks if you can help. Here's the whole page. PS - I'm also trying to see if it's possible to add the html markup, like if usernames are bold, etc. Thanks
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
/**
If this file is not in the FlashChat root folder, then change this
path to the location of the inc/common.php file.
*/
include_once('inc/common.php');
ChatServer::purgeExpired();
/**
Retrieves the number of users who are chatting in any room.
Leave the $room parameter empty to return the number of users in all room.
*/
function numusers( $room = "" )
{
if($room) {
$stmt = new Statement("SELECT COUNT(*) AS numb FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL AND roomid=?");
$rs = $stmt->process($room);
} else {
$stmt = new Statement("SELECT COUNT(*) AS numb FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL");
$rs = $stmt->process();
}
$rec = $rs->next();
return $rec?$rec['numb']:0;
}
/**
Retrieves a list of the users (by login ID) who are in $room.
Leave the $room parameter empty to return a list of all users in all rooms.
*/
function usersinroom( $room = "" )
{
$cms = $GLOBALS['fc_config']['cms'];
$list = array();
if($room) {
$stmt = new Statement("SELECT userid, state, color, lang, roomid FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL AND roomid=?");
$rs = $stmt->process($room);
} else {
$stmt = new Statement("SELECT userid FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL");
$rs = $stmt->process();
}
while($rec = $rs->next()) $list[] = array_merge($cms->getUser($rec['userid']), $rec);
return $list;
}
/**
Retrieves a list of all available rooms, as an array.
*/
function roomlist()
{
$list = array();
// populate $list with the names of all available rooms
$stmt = new Statement("SELECT * FROM {$GLOBALS['fc_config']['db']['pref']}rooms WHERE ispublic IS NOT NULL");
$rs = $stmt->process();
while($rec = $rs->next()) $list[] = $rec;
//result will be an array of arrays like ('id' => <room id>, 'updated' = <timestamp>, 'created' => <timestamp>, 'name' => <room name>, 'ispublic' => <public flag>, 'ispermanent' => <autoclose flag>)
return $list;
}
$rooms = roomlist();
$roomnumb = sizeof($rooms);
?>
<?php if($roomnumb) { ?>
<table border="0" cellpadding="1">
<tr>
<td><div class="smallfont"><?php
$users = usersinroom();
$arr = array();
foreach( $users as $user ) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
$lis = implode(", ", $arr);
echo $lis;
?> </div></td>
</tr>
</table>
<?php } ?>
Added to post:
foreach is a loop, not a conditional. Therefore:
$users = usersinroom();
$arr = array();
if (!empty($users)) {
foreach ($users as $user) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
} else {
echo "<b>No Members in Chat</b>";
}
Thanks, I used this and it works fine!
Anyone know how to add the html markups to the usernames? Like, if a name is bolded, it can show the bolded names in the who's chatting, so it matches the who's online?
Thanks!
Also, I didn't post the whole code before, so can you tell me if what you gave me as the fix is correct?
<tr>
<td><div class="smallfont"><?php
$users = usersinroom();
$arr = array();
if (!empty($users)) {
foreach ($users as $user) {
echo "<a href=\"member.php?userid=$user[userid]\">$user[login]</a>";
}
} else {
echo "<em>Chat is empty.</em>";
}
$lis = implode(", ", $arr);
echo $lis;
?> </div></td>
</tr>
Since I didn't give you the $lis part before, I want to make sure what you gave me is lined up properly.
thanks
Link14716
03-07-2005, 09:10 PM
The usergroup markup requires global.php from vBulletin to be included and then it is a function. I forgot the name of the function, though. I know it contains 'musername' and it in includes/functions.php, though.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.