我试图将完整的日历事件数据插入到 jQuery
中,在本例中它代表用户的生日。我正在从 MySQL
数据库中检索生日。让我们看一下脚本
php
:
if ($birthday_r = $connection->query("SELECT CONCAT(name,' ',surname),MONTH(birthday),DAY(birthday) FROM users")){
while ($birthday_row = $birthday_r->fetch_row()){
$birthday_array[] = array(
'title' => $birthday_row[0],
'start' => Date("Y") . "-" . $birthday_row[1] . "-" . $birthday_row[2]
);
}
$json = json_encode($birthday_array);
birthday_r->free();
}
那么您如何看待将此 json 编码信息存储到 $json
变量中
javascript
:
jQuery("#calendar").fullCalendar({ // initialize full calendar
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay'
},
<?php if (isset($json_encode)){echo "events: " . $json . ",";} ?>
viewDisplay: function(view){
var i = view.title.slice(-4);
},
renderEvent: function(event){
}
});
这在加载页面时效果很好,但我希望在更改议程后(我的意思是例如按下一个或上一个按钮),每年的生日都显示在日历中。为此,我建议使用 viewDisplay
和 renderEvent
函数,但我无法在 php 以及 javascript 中修改年份变量。 viewDisplay
函数变量 i
是年份的数值(比如 2012)。我的问题是以某种方式将 Date("Y")
和 i
变量更新为 renderEvent
函数。我也试过像这样将检索到的信息存储到 javascript 变量中 =>
在这个例子中认为在 php 中给出
'start' => $birthday_row[1] . "-" . $birthday_row[2] // php
// below JavaScript code
var json_obj = <?php if (isset($birthday_array)){echo $birthday_array;} ?>
var d = new Date();
for (var i=0;i<json_obj.length;i++){
json_obj[i] = d.getFullYear() + "-" + json_obj[i];
} // this scripts works as well but I can not manipulate after prev or next buttons are pressed on calendar
附言。我想伙计们,明白我想做什么,请帮助我如何去做。非常感谢:)
最佳答案
嗯,最简单的解决方案可能是更改您的 PHP 循环并为您的事件添加“多个”年份。
while ($birthday_row = $birthday_r->fetch_row()){
$yearBegin = date("Y");
$yearEnd = $yearBegin + 10; // edit for your needs
$years = range($yearBegin, $yearEnd, 1);
foreach($years as $year){
$birthday_array[] = array(
'title' => $birthday_row[0],
'start' => $year . "-" . $birthday_row[1] . "-" . $birthday_row[2]
);
}
}
两个缺点:
- 你不能管理不同的生日(所以一个人的生日可能会出现,甚至在他死后)
- 它导致成倍增加的成本
您还可以查看 demo重复事件以构建前端解决方案。
关于php - 每年将生日事件添加到 jQuery 完整日历中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306351/