Find the last comma

Hey,

I have a simple while statement echoing out a record like so:


while($pplrow = mysql_fetch_object($conferencepeople)):
         echo $pplrow->Pname.', '
endwhile;

So this displays names like so:

John, Jack, David,

The problem is that i need to remove the last comma, how can i do this?

Thanks

Of course! Thanks Scallio i ran into that problem and found that your suggestion works nicely :wink:

Thanks again


while ($pplrow = mysql_fetch_object($conferencepeople)):
   $array[] = $pplrow->Pname;
endwhile;
$pName = implode(",", $array);
echo $pName;

Thanks!

Do you mean like this:


 while($pplrow = mysql_fetch_object($conferencepeople)):
           $array = array($pplrow->Pname);
           $pName = implode(",", $array);
          
          echo $pName;
endwhile;

Because this totally removes the comma’s, i want to separate each name by a comma but remove the last one…

:wink:


while ($pplrow = mysql_fetch_object($conferencepeople)):
   $array = array($pplrow->Pname);
endwhile; 
$pName = implode(",", $array);
echo $pName;

Once you’ve echoed it, you can’t.
Instead, put the values in an array, and then implode the array with a comma as delimiter, and echo the result.

One addition if I may: make sure you initialize the array.


$array=array();
while ($pplrow = mysql_fetch_object($conferencepeople)):
   $array[] = $pplrow->Pname;
endwhile;
$pName = implode(",", $array);
echo $pName;

If you don’t and there are no people, PHP will not like you calling implode() on NULL, it is however fine with an empty array passed as parameter :slight_smile:

Thanks, that worked perfectly! :slight_smile:

Hey,

I am now getting an error:

Warning: implode() [function.implode]: Invalid arguments passed in /searchresults.php on line 27

Pointing to this line:


$pName = implode(",", $array)

:confused: