0

I have a list of data as json which I have decoded in php. I am trying to loop through the array to dynamically display its contents in a url which will display a google chart diagram in PHP. The contents of the data are saved in a php variable $value and $label with concatenation to join the string.

so my url looks somthing like this.

$url = 'https://chart.googleapis.com/chart?cht=p3&chd=t:' **. $value .** '&chs=375x150&chl=' **. $label .** '&chco=4D89F9,009900,FF9900,FF7F7F,FFD088,8FA3D6';

$url = urlencode(**$url**);

Everything looks fine except the image can not be rendered in the php file due to a trailing (,) colon sign.

Here is the full url after being encoded. In bold are the contents of $value

https://chart.googleapis.com/chart?cht=p3&chd=**t:0.3,1.4,65.3,1.0,4.3,0.5,**&chs=375x150&chl=CBD%7CCBN%7CTHC%7CCBC%7CCBG%7CTHCV%7C&chco=4D89F9,009900,FF9900,FF7F7F,FFD088,8FA3D6

You can see after the last value of 0.5 there is a trailing (,) comma sign and I dont know how to get rid of it. When I remove this last comma the image renders itself on the page.

Any help will be greatly appreciated.

Thanks

3
  • urlencode won't add commas. It'll be there already in $value, so you need to fix whatever's generating the string in $value. Commented Feb 20, 2014 at 17:57
  • You need to show us the code generating $value. Commented Feb 20, 2014 at 17:58
  • this is the code: $value .= $data['profiling'][0][$i]['value1']; Commented Feb 20, 2014 at 18:25

2 Answers 2

5

A simple rtrim() should be what you need.

rtrim($value, ',');

Just do it before you concatenate the URL.

PHP docs on rtrim()

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that and I am still getting the same error.
0

Ok I figured out my problem. And i feel pretty dumb for this, but I wasnt even connected to my remote server via ftp. So the changes made with the rtrim() function were not seen.

Anyways rtrim() did the job!.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.