1

I have the following code in html:

<? foreach($tst as $test) : ?>
    <?=$test->id?>,
<? endforeach ?>

and that will result as test1,test2,test3,

How avoid that last comma in simple method. I can't use complicated code in html like:

<? $i = 0; ?>
<? foreach($tst as $test) : ?>
    <?= $test->id ?>,
<? endforeach ?>
<? $i++; ?>
<? if($i != count($tst)) :?>
    ,
<? endif; ?>
1
  • 1
    Why are you doing it like this? Why dont you write the for loop in php and simply echo the HTML? Commented Dec 3, 2009 at 8:30

1 Answer 1

5

Use implode on an interim array:

<?php

$a= array();

foreach($tst as $test) {
 $a[]= $test->id;
}

echo(implode(', ', $a));

?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.