if..

Ask about a PHP problem here.
Post Reply
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

if..

Post by ashwood »

basically i have this..
[syntax=php]<?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> ";

}

?> [/syntax]

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
I would put a signature.. BUT, i don't have the time.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: if..

Post by Temor »

Scratch that, It worked, but it was horrible. I'll rewrite it.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: if..

Post 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..
I would put a signature.. BUT, i don't have the time.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: if..

Post 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.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: if..

Post 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
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: if..

Post by jacek »

If you put all of the users into an array, you can use impode() to put something between each element.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: if..

Post 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.

[syntax=php]<?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;

?>[/syntax]

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
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: if..

Post by ashwood »

works a treat thanks a lot :D

p.s:

can you explain what implode() does please :)
I would put a signature.. BUT, i don't have the time.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: if..

Post 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.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: if..

Post 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 :)
I would put a signature.. BUT, i don't have the time.
Post Reply