On a page I have a simple PHP
query . MySQL
The end result is a list.
The part that generates the result
while($row = mysqli_fetch_array($result)) {
echo "<a href='".$row['url']."'>".$row['Name']."</a>, ";
}
This generates something like this:
Abc123, Def456, Ghi789,
But I don't want that last comma:
Abc123, Def456, Ghi789
How can I alter the PHP
to achieve this?
A possible solution would be to save the results in a string and before writing it to the screen using
echo
, remove the last comma usingrtrim
ortrim
(because you can pass them a mask by specifying the string ", "):Another possible solution would be to save the text (without the comma) in an array and then do a
implode
join with the comma:You can use the
substr()
PHP function:It will remove the last character of the string.