【问题标题】:add birthday events into jQuery full calendar each year每年将生日事件添加到 jQuery 完整日历中
【发布时间】:2012-08-31 15:26:38
【问题描述】:

我正在尝试将完整的日历事件数据插入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 中修改 year 变量。在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

PS。我想伙计们,了解我想要做什么,请帮助我如何做到这一点。提前非常感谢:)

【问题讨论】:

  • 也许我错过了什么。您希望这些事件如何显示在日历上?它与有开始日期的常规活动不同吗?你能再解释一下吗?

标签: php javascript jquery mysql fullcalendar


【解决方案1】:

最简单的解决方案可能是更改您的 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 与重复事件以构建前端解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多