mysql data -> javascript (flotr)?

77 views
Skip to first unread message

geoffcox

unread,
Aug 4, 2008, 1:02:34 PM8/4/08
to Prototype & script.aculo.us
Hello,

Is it possible to use AJAX to get data from a mysql database (that
part I can do using php) and then use the data to create a bar chart
using javascript/flotr?

Cheers,

Geoff

Christophe Porteneuve

unread,
Aug 4, 2008, 1:16:28 PM8/4/08
to prototype-s...@googlegroups.com
'course it is, except if you mean "connect to MySQL directly from my web
page using Ajax." Then it's not: MySQL has no web interface.

You should just write a small PHP page or whatever to grab your
parameters, do the MySQL dance, and return, say, a nicely formatted JSON
chunk.

I guess the data are numerical, so MySQL will abide by the IEEE-754
conventions, I believe, and so does JavaScript. You should be in the
clear for JSON encoding. You'll probably even find PHP functions that
handle JSON serialization for you!

This aside, how you get the data (Ajax or otherwise) is entirely
irrelevant to what you do with it (feeding Flotr).

--
Christophe Porteneuve aka TDD
t...@tddsworld.com

Alex Hernandez

unread,
Aug 4, 2008, 3:30:35 PM8/4/08
to Prototype & script.aculo.us
you can fetch data using :
var myData;
var dataToGraph = new Ajax.Request(
'getMySqlData.php',
onSuccess: function(t){
myData = t.responseXML; // if you want to fetch XML,
otherwise this can be t.responseText, that you can turn directly into
a incrustable chunk of HTML, or even to get JSON data.
}
);
alert(myData)

in your getMySqlData.php script you have to connect to mySQL as any
other data connection you have previuosly done before, You have to
keep in mind that any echo "something"; that you write in your PHP
file, is sent direclty tou your ajax response.

hope this helps.

Alex

geoffcox

unread,
Aug 4, 2008, 6:09:22 PM8/4/08
to Prototype & script.aculo.us
Thanks Christophe - I have no so far used Jason - any good tutorial
that you can suggest?

Cheers

Geoff

geoffcox

unread,
Aug 4, 2008, 6:09:59 PM8/4/08
to Prototype & script.aculo.us
thanks for the ideas Alex - will give them a whirl soon.

Cheers

Geoff
> > Geoff- Hide quoted text -
>
> - Show quoted text -

Christophe Porteneuve

unread,
Aug 5, 2008, 3:15:36 AM8/5/08
to prototype-s...@googlegroups.com
geoffcox a écrit :

> Thanks Christophe - I have no so far used Jason - any good tutorial
> that you can suggest?

There's really nothing to it. Generating it in PHP can be done easily
enough by buuilding a nested associative array and pushing it through
json_encode (if you're using PHP >= 5.2.0 with the proper module):

http://php.net/json-encode

On the client side, you'll just grab the "responseJSON" property of your
response object (the first arg to your callback function in your
Ajax.Request call), which will be the corresponding nested JS object
structure.

geoffcox

unread,
Aug 5, 2008, 5:37:15 AM8/5/08
to Prototype & script.aculo.us
thanks for the info Christophe,

Cheers

Geoff

4linx

unread,
Aug 8, 2008, 12:12:04 PM8/8/08
to Prototype & script.aculo.us
Geoff,

The absolute best charts I've seen (and used) are from this developer:

http://www.maani.us/xml_charts/index.php?menu=Gallery

Very flexible and works well with php and mysql.

tom
Reply all
Reply to author
Forward
0 new messages