1

I want to display a ",(comma)" in between each records fetched from the db.eg:location1,location2,location3 etc

<table class="table table-striped table-condensed table-hover">
<?php 
    //var_dump($volunteer_panel_location);
    echo "<tr>"
       . "<td class='col-xs-4'>Interview Location </td>"
       . "<td>";
    foreach ($volunteer_interview_location as $vlocations)
    {
        echo "{$vlocations->location}";
    }
    echo "</td></tr>"
       . "<tr>"
       . "<td>Panel Location </td>"
       . "<td>";
    foreach ($volunteer_panel_location as $plocations)
    {
        echo "{$plocations->location}";
    }
    echo "</td>"
       . "</tr>";
?>
</table>

I tried this,but the records repeated

$pp=array();
foreach ($volunteer_panel_location as $plocations)
{
    $pp[]=$plocations->location;
    echo implode(",",$pp);
}
jmattheis
  • 10,494
  • 11
  • 46
  • 58
Ann
  • 471
  • 9
  • 22

3 Answers3

1

Try this

$locations = [];
foreach ($volunteer_panel_location as $plocations)
{
    $locations[] = $plocations->location;
}
echo implode(",",$locations);
jmattheis
  • 10,494
  • 11
  • 46
  • 58
1
$ar='';   
for ($i=0; $i <count($data) ; $i++) {
    $ar[]=$data[$i]->id;
}
$rt=implode(',',$ar);
echo $rt;

replace $data to your data.

$ar is an array.

I suggest calling implode outside of foreach function.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

try this

echo implode(",",$volunteer_panel_location);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59