我正在使用dnCalendar显示所有日期明智的事件。我从数据库中获取日期的所有事件并存储在数组中。我必须为所有事件编写一个foreach
循环,以便在日历中列出它们。
任何jQuery日历显然都会在document.ready方法中加载。我想动态打印所有事件日期,为此我必须在日历初始化函数中打印我的PHP数组,但我的问题是日历初始化在document.ready方法中。
我认为document.ready方法在文档准备好之前加载但是我在加载文档之后得到了我的PHP数组,所以我的问题是我们可以在jQuery的document.ready方法中打印一个php数组吗? / p>
$(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();
});
我想在javascript的事件部分打印事件日期,如下所示:
notes: [
{ "date": "2017-12-25", "note": ["Cristmas"] },
{ "date": "2017-12-31", "note": ["Year End"] },
{ "date": "2018-1-1", "note": ["Year STart"] }
],
我上面写的php foreach循环是否正确?任何帮助,谢谢。
更新
我已经编写了一个用于获取所有事件并成功获取数据的ajax调用,但是我的问题是在获取数据后如何在ajax成功方法中打印respose?
notes: [
$.ajax({
url: "<?= base_url() ?>index.php/ajax/getEventsList",
type: "POST",
data: "",
success: function(response){
console.log(response);
document.write(response);
}
})
],
ajax的回应是:
{ "date": "2017-12-28", "note": ["event1"] },{ "date": "2017-12-26", "note": ["event2"] },{ "date": "2017-12-27", "note": ["event3"] },{ "date": "2017-12-28", "note": ["event4"] },