daghost Posted November 6, 2010 Share Posted November 6, 2010 Hey, i got this code which takes info from mysql: while ($row= mysql_fetch_array($result)) { $title = $row["alertid"]; $title2 = $row["alerttext"]; $title3 = $row["alertdate"]; $bla = "{\"alertid\":\"$title\",\"alerttext\":\"$title2\",\"alertdate\":\"$title3\"},"; echo "$bla"; } it is displayed like this: [{"alertid":"1","alerttext":"text1","alertdate":"date1"},{"alertid":"2","alerttext":"text2","alertdate":"date2"},{"alertid":"3","alerttext":"text3","alertdate":"date3"},] I wanted to ask, how can I remove the last comma, after date3"}, but keep other commas..? Link to comment https://forums.phpfreaks.com/topic/217950-remove-last-comma/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2010 Share Posted November 6, 2010 rtrim Link to comment https://forums.phpfreaks.com/topic/217950-remove-last-comma/#findComment-1131126 Share on other sites More sharing options...
radar Posted November 6, 2010 Share Posted November 6, 2010 Basic example of how I take care of this would be like: foreach ($_POST['levels'] as $t){ if ($levels == '') { $levels = $t; } else { $levels .= ",".$t; } } In my situation I have a multi select which comes out in an array, but in order to store it in a database I have to break it down into a single string. So I check if it's blank and add information to $levels, then if there is more selected it adds the comma before the data that way there isnt a comma at the end of the data. Perhaps it'll work for you too Link to comment https://forums.phpfreaks.com/topic/217950-remove-last-comma/#findComment-1131139 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.