Jump to content


Photo

Delete last comma


  • Please log in to reply
5 replies to this topic

#1 FuG5333

FuG5333
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationGreenville, SC

Posted 04 April 2006 - 07:43 PM

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. :)

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 04 April 2006 - 07:57 PM

trim ($str, ', ');

You can avoid the problem 2 ways

1 ) add names except first with comma BEFORE the name

$newname = 'Pete'
$list = '';

list .= ($list=='') ? $newname : ', ' . $newname; 
echo $list;

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

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

echo join (', ', $list);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 FuG5333

FuG5333
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationGreenville, SC

Posted 04 April 2006 - 08:05 PM

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:
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
}
?>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 08:10 PM

The code snippet you posted doesn't show a list of names being created or displayed.

Ken

#5 FuG5333

FuG5333
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationGreenville, SC

Posted 04 April 2006 - 08:20 PM

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

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 08:34 PM

Ok, try something like this:
<?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>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users