Page 1 of 1

if..

Posted: Tue Jun 14, 2011 10:36 am
by ashwood
basically i have this..
<?php

include '../core/connection_details.php';

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_database");

$loggedin = mysql_query("SELECT `username`, `displayname`, `logged_in` FROM members WHERE logged_in='1'");
while($row = mysql_fetch_assoc($loggedin))
{
  $user_in = $row['displayname'];
$user_name = $row['username'];
 
echo " <a href='".$user_name."'>".$user_in."</a> ";

}

?> 
that works perfect how i want it.. the thing is.. as you can see when i echo out the user thats logged in i have put a space to seperate each logged in user but i dont want that i want a commer (,) after the username if there is another username after that one if you get me so say

ashwood

was online on his own.. no commer would show but if

ashwood & tim were logged in i want it to show as

ashwood, tim

if there was 3 people..

ashwood, tim, bob

if you get me? so only the commer if there is another username after that one.. like phpbb does if you go forum index then to the bottom of the page..

Thanks Ash

Re: if..

Posted: Tue Jun 14, 2011 10:49 am
by Temor
Scratch that, It worked, but it was horrible. I'll rewrite it.

Re: if..

Posted: Tue Jun 14, 2011 10:52 am
by ashwood
yeah that works but i have 2 users logged in at the minute..

http://www.aatm-online.co.uk/index.php?

you can see it goes user1, user2, after user 2 i dont want a , if you get me..

Re: if..

Posted: Tue Jun 14, 2011 10:56 am
by Temor
ashwood wrote:yeah that works but i have 2 users logged in at the minute..

http://www.aatm-online.co.uk/index.php?

you can see it goes user1, user2, after user 2 i dont want a , if you get me..
Yeah, I'll see what I can do.

Re: if..

Posted: Tue Jun 14, 2011 10:58 am
by ashwood
Temor wrote:
ashwood wrote:yeah that works but i have 2 users logged in at the minute..

http://www.aatm-online.co.uk/index.php?

you can see it goes user1, user2, after user 2 i dont want a , if you get me..
Yeah, I'll see what I can do.

okay cheers

Re: if..

Posted: Tue Jun 14, 2011 11:15 am
by jacek
If you put all of the users into an array, you can use impode() to put something between each element.

Re: if..

Posted: Tue Jun 14, 2011 11:30 am
by Temor
jacek wrote:If you put all of the users into an array, you can use impode() to put something between each element.
That's what I did.
<?php
 
include '../core/connection_details.php';
 
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_database");
 

	$username = array();
	while($row = mysql_fetch_assoc($loggedin))
{
	  $user_in = $row['displayname'];
$user_name = $row['username'];
	
	$username[] = '<a href="'. $user_name . '">' . $user_in . '</a>';
	

	
	
}
$username = implode(', ',$username);

 


echo $username;
 
?>
Took me a while because I got a strange error that somehow solved itself...

"Fatal error: [] operator not supported for strings"
Not sure what gave me that error, or why it suddenly disappeared :P

Re: if..

Posted: Tue Jun 14, 2011 12:19 pm
by ashwood
works a treat thanks a lot :D

p.s:

can you explain what implode() does please :)

Re: if..

Posted: Tue Jun 14, 2011 12:32 pm
by Temor
ashwood wrote:works a treat thanks a lot :D

p.s:

can you explain what implode() does please :)
http://php.net/manual/en/function.implode.php

Basically you implode an array with a character of choice and it returns a string with all the array elements, separated by the character you chose.

Re: if..

Posted: Tue Jun 14, 2011 12:43 pm
by ashwood
Temor wrote:
ashwood wrote:works a treat thanks a lot :D

p.s:

can you explain what implode() does please :)
http://php.net/manual/en/function.implode.php

Basically you implode an array with a character of choice and it returns a string with all the array elements, separated by the character you chose.

ahhh :)