Jump to content

How can I get rid of the last comma after the last array value?


Jackosn5

Recommended Posts

Couple of different approaches:

 

1.) Get a count of the records in the result set with mysql_num_rows. In the loop check to see if you are on the last row. Only echo the comma if you're not.

 

2.) Instead of echoing directly in the loop, either concatenate onto a string and then, outside the loop, extract everything except the last comma with substr or put everything into an array and use implode with a comma as the 'glue'.

Thank you, I solved the problem like this:

 

while($row = mysql_fetch_array($result)) {

          $news[] = '{"Id":'.$row[id].',"LinkText":"'.$row[LinkText].'","Url":"'.$row.'","Title":"'.$row[Title].'"}';

}

$news = implode (",", $news);

print $news;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.