Jump to content

Archived

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

FuG5333

Delete last comma

Recommended Posts

I can tell PHP to check an entry and then add a ',' after each name. I have two links to add and delete you from the signup list. They work. What I want is the last ',' deleted so they display correctly. Here's an example:

Just one name:
"Bob"

Two of more names:
"Bob, John, Pete"


Just need the last name to not show a comma... I've seen this before but can't google it since I can associate this with a term. :)

Share this post


Link to post
Share on other sites
[code]trim ($str, ', ');[/code]

You can avoid the problem 2 ways

1 ) add names except first with comma BEFORE the name

[code]$newname = 'Pete'
$list = '';

list .= ($list=='') ? $newname : ', ' . $newname;
echo $list;[/code]

2 ) add names to an array then join the array elements

[code]$newname = 'Pete'
$list[] = $newname;

echo join (', ', $list);[/code]

Share this post


Link to post
Share on other sites
Sorry for the newbie question. :) Having trouble fiting that in. Makes perfect sense, just don't know enough to incorporate it. Here's what I have:
[code]connect();
$sql = "SELECT * FROM calendar";
$query = mysql_query($sql);
while ( $row = mysql_fetch_array($query) )
    {
    ?>
<font="arial" size="2" color="#666666"><?php echo $row['first_name']?></font>
<br>
<?php
}
?>[/code]

Share this post


Link to post
Share on other sites
The code snippet you posted doesn't show a list of names being created or displayed.

Ken

Share this post


Link to post
Share on other sites
Right. It pulls those from a database. Everything works. What happens now is I put a "<br>" after the list and it just lists them like:
Bob
John
Pete

What I need to do is list like:
Bob, John, Pete

Share this post


Link to post
Share on other sites
Ok, try something like this:
[code]<?php
$sql = "SELECT * FROM calendar";
$query = mysql_query($sql);
$tmp  = array();
while ( $row = mysql_fetch_array($query) )
    $tmp[] = $row['first_name'];
    ?>
<font="arial" size="2" color="#666666"><?php echo implode(', ',$tmp); ?></font>
<br>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.