add birthday events into jQuery full calendar each year

送分小仙女□ 提交于 2019-12-02 15:22:01

问题


I'm trying to insert into jQuery full calendar event data, which in this case represents birthdays of users. I'm retrieving birthdays from MySQL database. Let's take a look at script

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();
}

Then how you see storing this json encoded information into the $json variable

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){

        }
    });

Tthis works nice when page is loaded, but I want that after changing agenda (I mean for example press next, or prev buttons) , for every year birthday be shown in calendar. For this purpose I've advised to use viewDisplay and renderEvent functions , but I can not modifying year variable how in php as well in javascript. in viewDisplay function variable i is numeric value of year (like 2012). My problem is to somehow update Date("Y") with i variable into renderEvent function . Also I've tried to store retrieved information into javascript variable like so =>

in this example considered that in php is given

'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. I think guys , understand what I'm trying to do, pls help me how to do that . Thanks a lot beforehand :)


回答1:


Well the easiest solutions may be to alter your PHP loop and add "multiple" years to your events.

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]
        );
    }
}

Two drawbacks :

  • you can't manage different birthdays (so a person's birthdate may occur, even after his dead)
  • it leeds into exponential costs

You may also take a look at the demo with repeating events to build a frontend solution.



来源:https://stackoverflow.com/questions/12306351/add-birthday-events-into-jquery-full-calendar-each-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!