FuG5333 Posted April 4, 2006 Share Posted April 4, 2006 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 More sharing options...
Barand Posted April 4, 2006 Share Posted April 4, 2006 [code]trim ($str, ', ');[/code]You can avoid the problem 2 ways1 ) 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 More sharing options...
FuG5333 Posted April 4, 2006 Author Share Posted April 4, 2006 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 More sharing options...
kenrbnsn Posted April 4, 2006 Share Posted April 4, 2006 The code snippet you posted doesn't show a list of names being created or displayed. Ken Link to comment https://forums.phpfreaks.com/topic/6596-delete-last-comma/#findComment-23950 Share on other sites More sharing options...
FuG5333 Posted April 4, 2006 Author Share Posted April 4, 2006 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:BobJohnPeteWhat I need to do is list like:Bob, John, Pete Link to comment https://forums.phpfreaks.com/topic/6596-delete-last-comma/#findComment-23956 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2006 Share Posted April 4, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.