Jump to content

remove last comma


daghost
 Share

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.