I am showing all events date wise for that I am using dnCalendar. I am getting all the events with dates from the database and storing in an array. I have to write a foreach
loop for all events to list out them in the calendar.
Any jQuery calendar obviously loads in the document.ready method. I want to print all the event dates dynamically and for that I have to print my PHP array in the calendar initialization function, but my problem here is the calendar initialization is in document.ready method.
I think document.ready method loads before the document is ready but I get my PHP array after loading the document only, so my question here is can we print a php array inside jQuery's document.ready method?
$(document).ready(function() {
var my_calendar = $("#dncalendar-container").dnCalendar({
minDate: "2016-01-15",
maxDate: "2100-12-31",
monthNames: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
monthNamesShort: [ 'Jan', 'Feb', 'Mar', 'Apr', 'Mey', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
dayNames: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
dayNamesShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
notes: [
<?php if(isset($cal_events) && !empty($cal_events)){
foreach($cal_events as $each_event){ ?>
{ "date": "<?php echo date('Y-m-d', strtotime($each_event['event_date'])) ?>", "note": ["<?php echo $each_event['event_title']?>"] },
<?php } }
?>
],
showNotes: true,
startWeek: 'monday',
dayClick: function(date, view) {
------------------
-------------
}
});
my_calendar.build();
});
I want to print the event dates in events part of javascript like below:
notes: [
{ "date": "2017-12-25", "note": ["Cristmas"] },
{ "date": "2017-12-31", "note": ["Year End"] },
{ "date": "2018-1-1", "note": ["Year STart"] }
],
Is my above written php foreach loop is correct or not? Any help, Thanks.
UPDATE:
I have written ajax call for getting all events and fetching the data successfully but my problem here is after fetching the data how to print the respose in ajax success method?
notes: [
$.ajax({
url: "<?= base_url() ?>index.php/ajax/getEventsList",
type: "POST",
data: "",
success: function(response){
console.log(response);
document.write(response);
}
})
],
ajax response is:
{ "date": "2017-12-28", "note": ["event1"] },{ "date": "2017-12-26", "note": ["event2"] },{ "date": "2017-12-27", "note": ["event3"] },{ "date": "2017-12-28", "note": ["event4"] },