0

I am using bootstrap full calendar ,from here i cann't pass the php varaiable insteed of start: new Date(y, m, d, 8, 30), i want pass the y insteed of $y and m insteed of $m,how can i do, here they will give today,for me don;t want like this

$(function () {

        /* initialize the external events
         -----------------------------------------------------------------*/
        function ini_events(ele) {
          ele.each(function () {

            // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
            // it doesn't need to have a start or end
            var eventObject = {
              title: $.trim($(this).text()) // use the element's text as the event title
            };

            // store the Event Object in the DOM element so we can get to it later
            $(this).data('eventObject', eventObject);

            // make the event draggable using jQuery UI
            $(this).draggable({
              zIndex: 1070,
              revert: true, // will cause the event to go back to its
              revertDuration: 0  //  original position after the drag
            });

          });
        }
        ini_events($('#external-events div.external-event'));

        /* initialize the calendar
         -----------------------------------------------------------------*/
        //Date for the calendar events (dummy data)
        var date = new Date();
        var d = date.getDate(),
                m = date.getMonth(),
                y = date.getFullYear();
        $('#calendar').fullCalendar({
          header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
          },
          buttonText: {
            today: 'today',
            month: 'month',
            week: 'week',
            day: 'day'
          },
         
          //Random default events
         events: [
           <?php
                $task = mysql_query("SELECT * FROM task");
                while($tsk = mysql_fetch_assoc($task)){
                 /* $start_date = $tsk['t_started_on'];*/
                  $start_date = "2016-05-10 9:00";
                  $d_t=explode(" ",$start_date);
                  $ex_d = $d_t[0];//2016-05-10
                  $date=explode("-",$ex_d);
                  $y=$date[0];
                  $m=$date[1];
                  $d=$date[2];
                  $ex_t = $d_t[1];//09:00
                  $time=explode(":",$ex_t);
                  $h = $time[0];
                  $m = $time[1];
           ?>
           {
                title: '<?php echo $tsk['t_title']?>',
                start: new Date(y, m, d, 8, 30),
                end: new Date(y, m, d, 16),
                allDay: false,
                backgroundColor: "#f56954", //red
                borderColor: "#f56954" //red 
           },
           <?php } ?>
        ],
          
          editable: true,
          droppable: true, // this allows things to be dropped onto the calendar !!!
          drop: function (date, allDay) { // this function is called when something is dropped

            // retrieve the dropped element's stored Event Object
            var originalEventObject = $(this).data('eventObject');

            // we need to copy it, so that multiple events don't have a reference to the same object
            var copiedEventObject = $.extend({}, originalEventObject);

            // assign it the date that was reported
            copiedEventObject.start = date;
            copiedEventObject.allDay = allDay;
            copiedEventObject.backgroundColor = $(this).css("background-color");
            copiedEventObject.borderColor = $(this).css("border-color");

            // render the event on the calendar
            // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
            $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
              // if so, remove the element from the "Draggable Events" list
              $(this).remove();
            }

          }
        });

        /* ADDING EVENTS */
        var currColor = "#3c8dbc"; //Red by default
        //Color chooser button
        var colorChooser = $("#color-chooser-btn");
        $("#color-chooser > li > a").click(function (e) {
          e.preventDefault();
          //Save color
          currColor = $(this).css("color");
          //Add color effect to button
          $('#add-new-event').css({"background-color": currColor, "border-color": currColor});
        });
        $("#add-new-event").click(function (e) {
          e.preventDefault();
          //Get value and make sure it is not null
          var val = $("#new-event").val();
          if (val.length == 0) {
            return;
          }

          //Create events
          var event = $("<div />");
          event.css({"background-color": currColor, "border-color": currColor, "color": "#fff"}).addClass("external-event");
          event.html(val);
          $('#external-events').prepend(event);

          //Add draggable funtionality
          ini_events(event);

          //Remove event from text input
          $("#new-event").val("");
        });
      });
   <!-- HTML code-->
  <div id="calendar"></div>  

## Update query ##  

        //Random default events
     events: [
       <?php
            $task = mysql_query("SELECT * FROM task");
            while($tsk = mysql_fetch_assoc($task)){
              $start_date = $tsk['t_started_on'];
              /*$start_date = "2016-05-10 9:00";*/
              $d_t=explode(" ",$start_date);
              $ex_d = $d_t[0];//2016-05-10
              $date=explode("-",$ex_d);
              $y=$date[0];
              $m=$date[1];
              $d=$date[2];
              $ex_t = $d_t[1];//09:00
              $time=explode(":",$ex_t);
              $h = $time[0];
              $m = $time[1];
       ?>
       {
          title: "<?php echo $tsk['t_title']?>",
          start: new Date(<?= $y ?>, <?= $m ?>,<?= $d ?>, <?= $h ?>, <?= $m ?>),
          end: new Date(y, m, 15, 10, 00),
          allDay: false,
          backgroundColor: "#0073b7", //Blue
          borderColor: "#0073b7" //Blue
       },
       <?php } ?>
    ],
      

 
kumaran jayam
  • 33
  • 1
  • 2
  • 8
  • What have you tried, and what have gone wrong when you tried? Also your $d variable will actually contain both the date and the timestamp for it like so "01 00:00:00" for example. If you do new Date( , m d, 8 30) etc. and look at the sourcecode of your site, what do you see? Is the year echoed as intended? – Ole Haugset May 10 '16 at 11:24
  • I tried like this start: new Date(= $y ?>, m, d, 8, 30), it will work, – kumaran jayam May 10 '16 at 11:32
  • And also i want to pass the remaining two variable $m and $d,i tried like this start: new Date(= $y ?>, = $m ?>,= $d ?>, = $h ?>, = $m ?>), it is not working – kumaran jayam May 10 '16 at 11:43
  • And what does it print to your source code when you do this? You have to look at what it actually creates to be able to backtrace the actual error you are encountering. – Ole Haugset May 10 '16 at 11:44
  • When i am doing this time $m and $d etc not passing,only $y it will be taken,remaining $m taken on 01 and $d taken 01 and $h taken on 01 $min taken 01 – kumaran jayam May 10 '16 at 12:08

2 Answers2

0

You can directly print the variable you got from your php code in the calendar declaration, just how you did with the title using simplified print tags <?= $y ?>, which are equivalent to <?php echo $y; ?> like so:

title: '<?php echo $tsk["t_title"]?>',
start: new Date(<?= $y ?>, <?= $m ?>, <?= $d ?>, 8, 30),
end: new Date(y, m, d, 16),
allDay: false,
backgroundColor: "#f56954",
borderColor: "#f56954"  

The server will print your value for that $y variable, and the client will interpret it as normal javascript.

As a guidance this would be a way to take the variables out of the string:

$dummyTimestamp = "2015-12-05 21:09";

$d_t=explode(" ",$dummyTimestamp);
$date = $d_t[0];
$time = $d_t[1];
$arrayDate=explode("-",$date);
$y=$arrayDate[0];
$m=$arrayDate[1];
$d=$arrayDate[2];
$arrayTime=explode(":",$time);
$h = $arrayTime[0];
$m = $arrayTime[1];

echo $y ." ". $m ." ". $d;

Note that this way is the most simple but is insecure aswell, I'd recommend you this post, I think it covers your problem really well and you can check out better practices. Another recommendation would be to get rid of mysql_* functions since they are deprecated and they've been deleted in PHP 7, for more details read this.

Hope this helps you.

Asur
  • 3,727
  • 1
  • 26
  • 34
  • I tried like this but not working start: new Date(, , , 8, 30), – kumaran jayam May 10 '16 at 11:00
  • @kumaranjayam Well, are your $y,$m,$d variables ok? I used this example of printing directly because is the most simple one, but is insecure, I'd really recommend you to check the post I linked. – Asur May 10 '16 at 11:04
  • I tried your code only,in that code $y it will taken bassed DB,remaining all varaibles npt passing ,wt can i do Mr @asurbernardo – kumaran jayam May 10 '16 at 12:16
  • @kumaranjayam yes sorry that was an example, not a complete solution I will edit it. – Asur May 10 '16 at 12:33
  • I tried your edited code but nothing happening – kumaran jayam May 10 '16 at 12:42
  • @kumaranjayam I think something might be wrong with your php variables, try echoing them elsewhere so you can check they return any valid value. – Asur May 10 '16 at 12:47
  • Month it will taken for next month,form DB $m it will come 05,but while showing output it will in 06 – kumaran jayam May 10 '16 at 12:50
  • Sorry if I don't get all you are saying but your English is a bit broken, if I understood well, you are getting a different value what you would expect but it is printing ok. Right? – Asur May 10 '16 at 12:54
  • Yes right,$m only it will make problem now. – kumaran jayam May 10 '16 at 12:59
  • Then according to the rules, you should make a new question to solve that since the question topic is solved, but to guide you I would say it is a loop problem, so check it twice :) – Asur May 10 '16 at 13:01
  • $m adding +1 ,like while fetching tables that time $m will coming in 05(MAY),while displaying start: new Date(= $y ?>, = $m ?>, = $d ?>, 8, 30),// here $m showing in 06(JUNE) – kumaran jayam May 10 '16 at 13:01
  • @kumaranjayam I would get rid of that loop if you are only looking for one result. – Asur May 10 '16 at 13:04
  • Sorry Mr @ asurbernardo i can not understand , i want change anything in my code – kumaran jayam May 10 '16 at 13:16
  • @kumaranjayam try deleting that `while` loop – Asur May 10 '16 at 13:32
  • Deleting while loop means nothing will happen and while (){} like this also $m not working properly ,writing like this means it wil work perfect start: new Date(= $y ?>, = $mon-1 ?>, = $d ?>, = $h ?>, = $min ?>), ,but this is not correct way right? – kumaran jayam May 10 '16 at 13:42
  • @kumaranjayam That might work, but by deleting the loop I meant leaving this `$tsk = mysql_fetch_assoc($task)` with no `while(){}` :) – Asur May 10 '16 at 13:54
  • Same month not working properly – kumaran jayam May 10 '16 at 13:59
  • @kumaranjayam I've done an example and for me it works perfectly... just check the edit at my answer and try to do the same. – Asur May 10 '16 at 14:22
  • I think i made some other mistake, still i am getting same problem – kumaran jayam May 10 '16 at 14:37
  • @kumaranjayam I don't know where is that coming from but I recommend you to make a new question about that problem and close this one since your question is already answered. – Asur May 10 '16 at 14:40
0

You can print it in your JS code, as long as your js code is embedded in a php file.

If not, an usual practice is to print your php variable value in a data- attribute for an element involved in... Then, you only have to retrieve that value, for example:

 <div id="aGoodIdForMe" data-new-date-y="<?= $y ?>"></div>

Then, in your JS code, no matter if it is embedded or is a script file loaded, you could use this:

 var y = $('#aGoodIdForMe').data('newDateY');

 {
     start: new Date(y, m, d, 8, 30)
 }

You can look how .data() works here

Muriano
  • 383
  • 5
  • 13