0

I am showing all events date wise for that I am using dnCalendar. I am getting all the events with dates from the database and storing in an array. I have to write a foreach loop for all events to list out them in the calendar.

Any jQuery calendar obviously loads in the document.ready method. I want to print all the event dates dynamically and for that I have to print my PHP array in the calendar initialization function, but my problem here is the calendar initialization is in document.ready method.

I think document.ready method loads before the document is ready but I get my PHP array after loading the document only, so my question here is can we print a php array inside jQuery's document.ready method?

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

I want to print the event dates in events part of javascript like below:

notes: [
        { "date": "2017-12-25", "note": ["Cristmas"] },
        { "date": "2017-12-31", "note": ["Year End"] },
        { "date": "2018-1-1", "note": ["Year STart"] }
       ],

Is my above written php foreach loop is correct or not? Any help, Thanks.

UPDATE:

I have written ajax call for getting all events and fetching the data successfully but my problem here is after fetching the data how to print the respose in ajax success method?

notes: [
  $.ajax({
          url: "<?= base_url() ?>index.php/ajax/getEventsList",
          type: "POST",
          data: "",
          success: function(response){
             console.log(response);
             document.write(response);
          }
   })
],

ajax response is:

{ "date": "2017-12-28", "note": ["event1"] },{ "date": "2017-12-26", "note": ["event2"] },{ "date": "2017-12-27", "note": ["event3"] },{ "date": "2017-12-28", "note": ["event4"] },
Prasad Patel
  • 707
  • 3
  • 16
  • 53
  • So long as the output from your PHP is syntactically correct JS code, then yes you can. – Rory McCrossan Jan 18 '18 at 18:24
  • 1
    I'd recommend using `json_encode` rather than string-ing it together by hand – iainn Jan 18 '18 at 18:25
  • 2
    `I think document.ready method loads before the document is ready` this is not possible, by the very definition of the event. `I get my PHP array after loading the document only` this is not also not possible as PHP is executed on the server, long before any code is even sent to the client. – Rory McCrossan Jan 18 '18 at 18:26
  • Correct Rory. I'd suggest using ajax so get the results then. – Gavin Simpson Jan 18 '18 at 18:35
  • You can only do this if you are creating the Javascript out of a PHP script. If you mean can you run PHP code when the Javascript is running in the browser then No you cannot – RiggsFolly Jan 18 '18 at 18:42
  • hey Thanks. looks like your idea will work, I will try using ajax @Gavin Simpson – Prasad Patel Jan 18 '18 at 19:24
  • Hello Gavin, I have updated my question, could you please check it once & tell me how to print the ajax response? @Gavin – Prasad Patel Jan 18 '18 at 19:44
  • You are very close. Create a 'div' in your document (eg "
    ", then format the response and do "('#mydiv").html(response);". That will put the response into the div. There are many questions and answers on stackoverflow about this.
    – Gavin Simpson Jan 19 '18 at 04:13
  • Thanks but I knew this(appending response to a div by it's id), I did several times in our projects. What I want here is I have to print (or) echo the response then & there only in the ajax success method only like `notes: [ { "date": "2017-12-25", "note": ["Cristmas"] }, { "date": "2017-12-31", "note": ["Year End"] }, { "date": "2018-1-1", "note": ["Year STart"] } ],`. I think it is possible with php's echo only, something like – Prasad Patel Jan 19 '18 at 04:54
  • take a look at https://stackoverflow.com/questions/32348455/getting-ajax-script-response-in-php-variable and see if it helps. I don't know offhand. – Gavin Simpson Jan 20 '18 at 11:09

0 Answers0