Skip to main content
Inspiring
August 10, 2008
Question

How to omit the last comma

  • August 10, 2008
  • 8 replies
  • 676 views
In a repeat region I have a comma separated list of email recipients:

<?php do { ?>
<?php echo $row_rsEmailbcc['email']; ?>,
<?php } while ($row_rsEmailbcc = mysql_fetch_assoc($rsEmailbcc)); ?>

What should I do to omit the last comma which is displayed at the end of
the select?

Leolux
This topic has been closed for replies.

8 replies

Inspiring
August 10, 2008
.oO(Leolux)

>Michael Fesser wrote:
>> Just a missing array index. The above code appends the entire record (an
>> array) to the $bcc variable, while all you want is the 'email' entry.
>> Instead of
>> $bcc[] = $row_rsEmailbcc;
>> try
>> $bcc[] = $row_rsEmailbcc['email'];
>
>Yes, that is it - thank you. That works fine. So David and you changed
>the record into an array []. In that array all the values of the
>recordset are written. If I want a specific field out of the recordset I
>have to specify the array and call it directly with its appropriate name.

In your code you simply looped through the entire record set and
directly printed each record's email field and a comma. This led to your
trailing-comma-problem.

In our version (actually I just gave the keywords, while David supplied
the implementation) the email addresses are not printed directly, but
collected in an array. This array is then printed after the loop with
the help if implode().

Micha
Inspiring
August 10, 2008
Michael Fesser wrote:
> Just a missing array index. The above code appends the entire record (an
> array) to the $bcc variable, while all you want is the 'email' entry.
> Instead of
> $bcc[] = $row_rsEmailbcc;
> try
> $bcc[] = $row_rsEmailbcc['email'];

Yes, that is it - thank you. That works fine. So David and you changed
the record into an array []. In that array all the values of the
recordset are written. If I want a specific field out of the recordset I
have to specify the array and call it directly with its appropriate name.

Leolux
Inspiring
August 10, 2008
.oO(Leolux)

>> David Powers wrote:
>> <?php do {
>> $bcc[] = $row_rsEmailbcc;
>> } while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc));
>> echo implode(',', $bcc);
>> ?>
>
>Thank you for the correction. UNfortunately that results in:
>
>Notice: Array to string conversion in ... on line 212 (repeats 16times)
>
>and in the end
>
>Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array
>
>OK - there are 16 records and Array is displayed 16times. But where are
>the records hidden?

Just a missing array index. The above code appends the entire record (an
array) to the $bcc variable, while all you want is the 'email' entry.

Instead of

$bcc[] = $row_rsEmailbcc;

try

$bcc[] = $row_rsEmailbcc['email'];

HTH
Micha
Inspiring
August 10, 2008
> David Powers wrote:
> <?php do {
> $bcc[] = $row_rsEmailbcc;
> } while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc));
> echo implode(',', $bcc);
> ?>

Thank you for the correction. UNfortunately that results in:

Notice: Array to string conversion in ... on line 212 (repeats 16times)

and in the end

Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array

OK - there are 16 records and Array is displayed 16times. But where are
the records hidden?

Leolux
Inspiring
August 10, 2008
David Powers wrote:
> $bcc[] = $row_rsEmailbcc);

Just noticed a typo. That closing parenthesis shouldn't be there.

<?php do {
$bcc[] = $row_rsEmailbcc;
} while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc));
echo implode(',', $bcc);
?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
August 10, 2008
Leolux wrote:
> <?php do {
> echo implode(",",$row_rsEmailbcc);
> } while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc)); ?>
>
> lists the results without space and comma.
> Is at least the approach correct?

No, not really. $row_rsEmailbcc is a single item, not an array.

<?php do {
$bcc[] = $row_rsEmailbcc);
} while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc));
echo implode(',', $bcc);
?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
August 10, 2008
Michael Fesser wrote:
>
> Have a look at arrays and implode().
>

<?php do {
echo implode(",",$row_rsEmailbcc);
} while ($row_rsEmailbcc= mysql_fetch_assoc($rsEmailbcc)); ?>

lists the results without space and comma.
Is at least the approach correct?

Leolux
Inspiring
August 10, 2008
.oO(Leolux)

>In a repeat region I have a comma separated list of email recipients:
>
><?php do { ?>
><?php echo $row_rsEmailbcc['email']; ?>,
><?php } while ($row_rsEmailbcc = mysql_fetch_assoc($rsEmailbcc)); ?>
>
>What should I do to omit the last comma which is displayed at the end of
>the select?

Have a look at arrays and implode().

Micha