remove last seperator from array while loop


#1

Hi,

I’m having an issue, I’m trying to display mysql data with a php query in some javascript to display a chart.

this is my php:

[php]$query = “SELECT * FROM stats”;
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row[‘values’] . ", ";
}[/php]

The return generates the following:

10000, 20000, 15000, 5000, 20000, 23000, 10000,

That result has to remove the last ", " so the new result is:

10000, 20000, 15000, 5000, 20000, 23000, 10000

Can someone plz help?
Thanks


#2
hello Chunter, use below code [php]

$query = “SELECT * FROM stats”;
$result = mysql_query($query) or die(mysql_error());
$output= array();
while($row = mysql_fetch_array($result)){
$output[] = $row[‘values’];
}
$finaloutput = implode(’,’,$output);
echo $finaloutput;

[/php]

i hope this helpful for you.
there is also other ways to do this but this best way according to me.
SR


#3

Thanks for the quick help, however I think something has gone wrong. When I use your code i get the following error:

Notice: Undefined index: values in H:\xampp\htdocs\stats\highcharts.php on line 120
,

Can’t seem to find the error


#4

Sarthak is right, that should work well for you.
Post the new version of your code for that section.
You must have spelled something wrong.


#5
hello Chunter,

i think value you are saving in to $output array is wrong
once check that $row[‘values’]; providing correct result to you.
than print $output as print_r($output)
before
$finaloutput = implode(’,’,$output);

i hope this will helpful for you.
SR