Search code examples
jquerymysqlfullcalendarfullcalendar-scheduler

fullcalendar mysql select WHERE current date range


I have fullcalendar working with mysql database using php to fetch data from the database. I am trying to modify the select with data from fullcalendars 'getview'. I need the current views first day of the month and I am trying to implement this code:

resources: 
            { 

                url: 'resources.php',
                data: {'period_start': moment( $("#calendar").fullCalendar("getView").intervalStart ).format("YYYY-MM-DD"), 'period_end': moment( $("#calendar").fullCalendar("getView").intervalEnd ).format("YYYY-MM-DD") } ,
            //  data:{'period_start':test},
                type: 'POST',

                error: function() {
                    $('#script-warning').show();
                }
            },

Unfortunately this always gives me the current date not the start of the view or anything else. what am I missing here?

everything goes to mysql perfectly and the select works with post. the only thing is that it will always give me the today's date, which is useless.

If i use this code, it works perfectly. when clicking on an event, it will give me the current view's first day:

eventClick: function(event) {
        alert(moment( $("#calendar").fullCalendar("getView").intervalStart ).format("YYYY-MM-DD"));
            }, 

EDIT:

I added a function:

function start(){
var view =$("#calendar").fullCalendar("getView").start;
return view};

When I use start() in eventclick, it works perfectly, but not when I use in resources. I am guessing I have to build the resources up as a function (link) but I am too noob to understand this. Can I even somehow use function to get the feed from mysql?


Solution

  • I managed to figure out this half-a$$ solution. It will do, but it requires the user to make some unnecessary refreshes to get the latest info.

    function start(){return Cookies.get('fullCalendarStart');};
    
    $('#calendar').fullCalendar({
    viewRender: function(view) {
                Cookies.set('fullCalendarStart', view.intervalStart.format(), {path: ''});
                },
    
    resources: 
                { 
                    url: 'resources.php',
                  data:{'period_start':start()},
                    type: 'POST'
                }
    
    {)
    

    This will post the view's start date to resources.php post, where I can take it and use it in mysql select. You can add whatever you like from Fullcalendar, put it in cookies and use in mysql select this way.

    Unfortunaly this is not the perfect solution and I'm still hoping to get it working without cookies.

    EDIT

    And a better solution:

    resources: function(callback){
                        setTimeout(function(){
                        var view = $('#calendar').fullCalendar('getView');
                        $.ajax({
                url: 'feed.php',
                dataType: 'json',
                cache: false,
                data: {
                    start: view.start.format(),
                    end: view.end.format(),
                    timezone: view.options.timezone
                    }
    
                }).then(function(resources){callback(resources)});      
            },0);
        },
    

    This will add start and end parameters like when fetching events. You can add $feed_start = $_GET['start']; to feed.php and use the variable '$feed_start' in mysql select. I got the input from https://github.com/fullcalendar/fullcalendar-scheduler/issues/246?_pjax=%23js-repo-pjax-container