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
https://forums.phpfreaks.com/topic/6596-delete-last-comma/
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
https://forums.phpfreaks.com/topic/6596-delete-last-comma/#findComment-23942
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
https://forums.phpfreaks.com/topic/6596-delete-last-comma/#findComment-23947
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
https://forums.phpfreaks.com/topic/6596-delete-last-comma/#findComment-23969
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.