Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

How to omit the last comma

LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

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
TOPICS
Server side applications

Views

654
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

.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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

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/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

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/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

> 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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

.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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2008 Aug 10, 2008

Copy link to clipboard

Copied

LATEST
.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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines