This question already has an answer here:
I want to remove the last record comma from fetched value. I used $album1 = substr($album,0,-1);
to remove last character comma from the value `$ album'. I caught:
SyntaxError: Unexpected token ,'
Whats wrong with this in substr()
?
$album = {"media_type":"image/png","pic_id":"zhadb"},
{"media_type":"image/png","pic_id":"zhadb"},
{"media_type":"image/video","pic_id":"kg5k4"},
while($fet_pic=mysql_fetch_array($albpic)) {
$album.=$fet_pic['CONTENT_VALUE'].',';
}
$album1 = substr($album,0,-1);
</div>
you can uses trim
to remove last comma from the string
like this
$album1 = trim($album, ',');
Try with following code :
$rows = array();
while($fet_pic=mysql_fetch_array($albpic))
{
$rows[] = $fet_pic['CONTENT_VALUE'];
}
$album = implod(", ",$rows);
use PHP rtrim
Strip whitespace (or other characters) from the end of a string
rtrim($album, ",")
Full code
<?php
$album = '{"media_type":"image/png","pic_id":"zhadb"},
{"media_type":"image/png","pic_id":"zhadb"},
{"media_type":"image/video","pic_id":"kg5k4"},';
echo rtrim($album, ",");