Getting Database Data to the client side

507 Views Asked by At

I'm sure this has been asked a million times, but what is the accepted approach to this?

I have been writing php code for a while and up until recently I only copied+pasted javascript code, but now with help from YUI I've begun to understand javascript and want to use it more in an existing web app I have. I want to get various amounts of data from databases etc to the clientside javascript. I have access to this data in my php pages when loading.

What is the correct way to get this data to my client side script.

  1. Generate clientside javascript in my php or smarty template files inserting the data where I need it?
  2. Use an Ajax call to retrieve the information I need from a php file - returning JSON data?
  3. Generate a JSON data construct of the data needed by the page and either return it via a script inclusion or dumping it to the generated page?
  4. Something really obvious I haven't thought of.
3

There are 3 best solutions below

0
On BEST ANSWER

If you can send the data with the page, I'd do that, (That is, number one) but retrieving with Ajax is probably the best-choice when the page has already been loaded.

0
On

You are probably using YUI Data Table I would assume, if the data set isn't huge and you have it at page request time, stick it in a local JS variable (I prefer using JSON format) and wrap a YAHOO.util.LocalDataSource around it, hook that up to the Data Table and you are set.

0
On

If you consistently use a good array structure for your data, then you can use both generated pages and ajax - either use the array to output the html (straight up php, smarty, whatever) and then you can return that data using json_encode() for ajax stuff. That way, if the user has no javascript your page still works :)