FLOT from MySQL via PHP, just cannot get it!

1,330 views
Skip to first unread message

gstar

unread,
Feb 15, 2012, 9:44:24 AM2/15/12
to Flot graphs
Good morning, hope someone can take pity on me please! I want to use
FLOT to display data from a recordset I have created in php with mysql
backend.

So far I have created flottest.php which contains the recordset and
table output, nothing else on this page. Below is the table output &
recordset is called rs_Orders?

<table border="0">
<caption>2009 Employee Sales by Department</caption>

<tr>
<th>Total Cases </th>
<th>Total Commission</th>
<th>Advisor Name</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rsMI_Direct['Count(tblcases.CaseDate)']; ?></
td>
<td>£ <?php echo
$row_rsMI_Direct['Sum(tblcases.CaseCommission)']; ?></td>
<td><?php echo $row_rsMI_Direct['ContactFullName']; ?></td>
</tr>
<?php } while ($row_rsMI_Direct =
mysql_fetch_assoc($rsMI_Direct)); ?>
</table>


How do I proceed to use JSON or another php page to display the FLOT
graphs, I know thats a basic request but I have been searching for
this now for days with no success.

I did check the demos but I cant see where/how you are making the JSON
files or where to pull them into the pages?

Many thanks

Gary

rabidkangaroo

unread,
Feb 16, 2012, 4:04:34 PM2/16/12
to Flot graphs
First you need to close up your php commands.

I'm not an expert on JSON, but I do have a robust and working solution
using MySQL, PHP, and FLOT to graph a complicated dataset.

I don't use JSON, at least not directly. PHP is quite powerful to run
out and capture data from MySQL and allow you to put the data into
javascript arrays.

The following is the beginning of my html and php code:
<? ////////////////connect to database
if(!mysql_connect("localhost","root",""))
{
echo "<h2>connection failure</h2>";
die();
}
mysql_select_db("BNI");
?>

I recommend the book: MySQL/PHP Database Applications by Jay Greenspan
and Brad Bulger. Simple to follow instructions. (I'm not associated
with either author or publishing house.) That will at least get you
started on mixing up html and php code.

gstar

unread,
Feb 20, 2012, 4:15:44 AM2/20/12
to Flot graphs
Has anyone any working examples I could perhaps take a look at, cant
believe how difficult its become trying to track down this
information. I will even pay if someone can assist me here?

Cheers

Tracy Aichele

unread,
Feb 20, 2012, 7:43:04 AM2/20/12
to flot-...@googlegroups.com
You should put your database data into your javascript. Look at the
basic example on:
http://people.iola.dk/olau/flot/examples/basic.html

You set up a placeholder div. Then you create a javascript somewhere
after creating the div (here's the example js) - you'll just need to
create the script dynamically (specifically, d1, d2, d3 here) with
your dynamic data:

<script type="text/javascript">
$(function () {
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);

var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

// a null signifies separate line segments
var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];

$.plot($("#placeholder"), [ d1, d2, d3 ]);
});
</script>

--
Tracy Aichele
tracy....@gmail.com
517-648-1967

bi5on

unread,
Feb 20, 2012, 10:36:39 AM2/20/12
to flot-...@googlegroups.com
take a look at thechels.co.uk for using php and flot.

i can provide code examples if you wish.


Juan Mendes

unread,
Feb 21, 2012, 8:38:34 PM2/21/12
to flot-...@googlegroups.com
Gary,


It's hard to tell what you are asking, but it looks like you just need a way to convert your db records into the JSON array format that is expected by Flot.

You said the record set is called rs_Orders but your code calls it rsMI_Direct. Here are the steps, my syntax is not (even close to) correct but it should give you an idea

<?php
$flotData = array();

while ($row = mysql_fetch_assoc($rsMI_Direct))) {
    // Add your data here, fetching it from the record, the following code would create three separate series
    $flotData[] = array(
          array($row['Date'], $row['Sales']),
          array($row['Date'], $row['SalesPerson']),
          array($row['Date'], $row['AnotherField']),
     );

}

?>


var flotData = <?php echo json_encode($flotData) ?>;

$.plot($("#placeholder"),flotData, {... your options here...});

Regards,

--Juan Mendes



You just need to turn your 

On Mon, Feb 20, 2012 at 7:36 AM, bi5on <mat.th...@gmail.com> wrote:
take a look at thechels.co.uk for using php and flot.

i can provide code examples if you wish.





--
Juan Mendes

gstar

unread,
Feb 22, 2012, 9:41:16 AM2/22/12
to Flot graphs
Hi there, that's exactly what I want to achieve, have you a very basic
example with perhaps 1 simple mysql table, 1 x json & 1 x php? If I
could play about with a working demo I am sure it would help
massively..

Thanks for your time..

Igor Ganin

unread,
Feb 22, 2012, 12:26:52 PM2/22/12
to flot-...@googlegroups.com
Hi, please find a nice example here:
http://openenergymonitor.org/emon/node/107

BR

2012/2/22 gstar <gary....@gmail.com>:

--
Best regards,
 Igor Ganin

Reply all
Reply to author
Forward
0 new messages