tags:

views:

44

answers:

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 cant use complicated code in html like

<? $i = 0 ;?>
<? foreach($tst as $test) : ?>
<?=$test->id?>,
<? endforeach ?>
<? $i++ ;?>
<? if($i != count($tst)) :?>
,
<?endif;?>
<? endforeach;?>

Please Help :)

+4  A: 

Use implode on an interim array:

<?php

$a= array();

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

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

?>
pygorex1