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
https://forums.phpfreaks.com/topic/212431-text-formatting/
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
https://forums.phpfreaks.com/topic/212431-text-formatting/#findComment-1106784
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.