Passing PHP variables to Javascript Array

I have the following code for a Javascript Calendar I want to use. var events_array = new Array( { startDate: new Date(2011,07, 20, 15, 50), endDate: new Date(2012,00, 10), title: "Event 1", description: "", priority: 2, // 1 = Low, 2 = Medium, 3 = Urgent frequency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly }, { startDate: new Date(2011,07, 20, 16, 50), title: "Event 2", description: "Description 2", priority: 1 }, { startDate: new Date(2012,07, 20, 9, 50), endDate: new Date(2012,00, 20), title: "Event 3", description: "Description 3", priority: 3, frequency:3 } ); $("#calendar").dp_calendar({ events_array: events_array, show_sort_by: false }); });

Rather than having to manually enter these events, I have setup the system to save these variables in a table called ‘events’. I can echo these entries on page by using a simple echo like [php]<?php echo $events['startDate']; ?>[/php] but how can I get the echo to create the javascript array? Can anyone help me please?

You can probably echo a JSON encoded version of the table and then have Javascript parse it.

<script type='text/javascript'>
var event_table_json = <? echo json_encode($events) ?>;
var calendar = jQuery.parseJSON(event_table_json);

// process

</script>
Sponsor our Newsletter | Privacy Policy | Terms of Service