Search code examples
phpmysqljpgraph

Need line breaks in database display for jpGraph


I'm learning how to use jpGraph to display bar graphs. The instructions explain how to create a .txt file with the following data:

1700.5   5.0  
1701.5  110.0  
1702.5  16.0  
1703.5  23.0  

I discovered that it still works if I change the extension to .php. But rather than use a static file, I want to figure out how to display data from a database table. Specifically, I want to display dates and test scores, like this:

2015-01-05  10
2015-01-05  50
2015-01-21  80

So I replaced the static data with a database query, followed by this code...

while ($row = $stm->fetch())
{
 $Test_Score = $row['Test_Score'];
 $Test_Date = $row['Test_Date'];

 $Results[] = ''.$Test_Date.'  '.$Test_Score.'';
}

echo join ($Results, '');

The problem is that this displays something like this...

2015-01-05  102015-01-05  502015-01-21  80

Adding a break doesn't help, apparently because it isn't compatible with jpGraph's code...

$Results[] = ''.$Test_Date.'  '.$Test_Score.'<br>';

So I'm trying to figure out an alternative way to insert line breaks after each row of code. I'm on a Mac, so I think I'm supposed to use /r/n, but I've also tried /r and /n, enclosed in both double quotes and single quotes.

What's the magic formula?


Solution

  • Try it:

    $lines = implode(PHP_EOL,$Results); // equal join(PHP_EOL,$Results);
    

    Constant PHP_EOL automatically set correct line-break of current OS.