I have a MySQL database which have simple tables like this:
colorTable
label | price | name
---------------------
red 10 Red
blue 20 Blue
sizeTable
label | price | name | available
---------------------------------
small 5 Small Yes
med 10 Med No
I want to load those variables into javascript variables so that they are an array of objects like this:
var colorTable = [
{label:'red', price: '10', name: 'Red'},
{label:'blue', price: '20', name: 'Blue'}
];
var sizeTable = [
{label:'small', price: '5', name: '10', available: 'Yes'},
{label:'med', price: '10', name: 'Med', available: 'No'}
];
I was able to load them in by outputting the variables into a plain .php file and loading that file as a javascript file, but something tells me there is a better way and I have no idea what that way would be?
I'm fine using plain javascript or jQuery, whichever is easiest.
It would need to loop through an unlimited number of tables (and columns within those tables) to automatically pull data for every table added to the database.
json_encode()the array you're retrieving from your table?echo json_encode(mySqlResults);then call that file with ajax and process the results exactly like you want