-1

I have a PhP variable like

<?php 
  $query=mysql_query("SELECT * FROM visited_city WHERE username='$user' LIMIT 3");     

  while($row=mysql_fetch_assoc($query)){ 
     $city=$row['city']; 

     echo '<a href="'.$city.'">'.$city.', </a>'; 
  }
?> 

Results=

Dhaka, Delhi, Mumbai,

Actually, I want like

Dhaka, Delhi, Mumbai

I am sorry, there are so many answers I found but did not match me...

1 Answers1

1

suppose that the list of cities is an array $cities then

<?php
$counter = 0;

// the list of cities to be printed
$list = "";

foreach($cities as $city) {
    if($counter == count($cities)) {
        $list.= $city;
    } else {
        $list.= $city.",";
    }
        $counter++;
}

echo $list;
?>
Mohamad Attat
  • 493
  • 2
  • 8