Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop - last result trim 2

Status
Not open for further replies.

maharg

Technical User
Mar 21, 2002
184
Hi

I am trying to create data for a Google Graphs application, taking data from MySQL

For example, if my data contains 6 lines of values, I need to create my data for the graph as follows ...

['Time from Andy', Graph value, Down Time],
['Time from Andy', Graph value, Down Time],
['Time from Andy', Graph value, Down Time],
['Time from Andy', Graph value, Down Time],
['Time from Andy', Graph value, Down Time],
['Time from Andy', Graph value, Down Time]

The last line must have no terminating comma.


I started off with this code, before I realised that the last comma must be absent (Or IE* fails to render the graph)

You can see an example in
Code:
while($row=mysql_fetch_array($result))
{
extract($row);
$time_label = $row['TimeFromAndy']; 
$graph_value = $row['InstRate']; 
$downtime_value = $row['DownTime']; 

echo "['".$time_label."', ".$graph_value.", ".$downtime_value."],".PHP_EOL;
}

I am struggling to see how to eliminate the terminating comma from just the last output line, so that it won't goof out IE.

Any guidance would be much appreciated!

With best regards,

Graham
 
Hi

Is the output supposed to be JSON ? If yes, use [tt]json_encode()[/tt] :
PHP:
[teal]<?php[/teal]
[navy]$data[/navy][teal]=[/teal][b]array[/b][teal]();[/teal]
[b]while[/b] [teal]([/teal][navy]$row[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_fetch_array[/color][teal]([/teal][navy]$result[/navy][teal]))[/teal]
  [navy]$data[/navy][teal][]=[/teal][b]array[/b][teal]([/teal][navy]$row[/navy][teal][[/teal][green][i]'TimeFromAndy'[/i][/green][teal]],[/teal][navy]$row[/navy][teal][[/teal][green][i]'InstRate'[/i][/green][teal]],[/teal][navy]$row[/navy][teal][[/teal][green][i]'DownTime'[/i][/green][teal]]);[/teal]

[b]echo[/b] [COLOR=darkgoldenrod]json_encode[/color][teal]([/teal][navy]$data[/navy][teal]);[/teal]

Feherke.
 
Code:
[COLOR=#009900 ]$o[/color][COLOR=#990000 ]=[/color][b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]();[/color]
[b][COLOR=#0000FF ]while[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]=[/color][b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$result[/color][COLOR=#990000 ]))[/color][COLOR=#FF0000 ]{[/color]
 [COLOR=#009900 ]$o[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"['{$row['TimeFromAndy']}',{$row['InstRate']},{$row['DownTime']}]"[/color][COLOR=#990000 ];[/color] 
[COLOR=#FF0000 ]}[/color]
[b][COLOR=#0000FF ]echo[/color][/b] [b][COLOR=#000000 ]implode[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]','[/color][COLOR=#990000 ],[/color][COLOR=#009900 ]$o[/color][COLOR=#990000 ]);[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top