Jump to content

text formatting


doddsey_65

Recommended Posts

I want a little online users list at the bottom of my page and the color of the username is to match the color defined in the database. but how do i get them to align side by side? if i add them to a <p> they will all appear under neath each other. Heres my current code:

 


$sql=mysql_query("SELECT * FROM ".DB_PREFIX."members WHERE online = 1");
while ($row = mysql_fetch_object($sql)) {

echo '<p style="color: #'.$row->username_color.';">'.$row->username.'</p>';

}

 

also i want a comma at the end of each but how do i stop it adding a comma to the end of the last one?

 

Link to comment
Share on other sites

Don't, use <p> use an inline element such as <span> instead.

 

To display the usernames in a comma delimited list there are different ways you can do this. Here are a few

Option1

$usrers = '';
while ($row = mysql_fetch_object($sql))
     $users .= '<span style="color: #'.$row->username_color.';">'.$row->username.'</span>, ';

// removes the last two character from the end of the string (which will be the comma and a space)
echo '<p>' . sub_str($users, 0, -2) . '</p>';

 

Or

$users = array();
while ($row = mysql_fetch_object($sql))
     $users[] = '<span style="color: #'.$row->username_color.';">'.$row->username.'</span>'; // add all users to the users array

// implode the users into a comma delimited string.
echo '<p>' . implode(', ', $users) . '</p>';

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.