Jump to content

Delete last comma


FuG5333

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. :)
Link to comment
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]
Link to comment
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]
Link to comment
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
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.