how to get values from db and insert them to events array

时间:2016-03-02 10:58:37

标签: javascript php laravel-5 fullcalendar

My code is as below.

<script>
    var t=<?php echo json_encode($ta)?>;
    var d=<?php echo json_encode($da)?>;

    $(document).ready(function() {

        $('#calendar').fullCalendar({

            //defaultDate: '2016-03-12',
            editable: true,
            eventLimit: true, // allow "more" link when too many events

            //$r=$ev->title;

            for(var j=0;j<d.length;j++)
            {
                events: [{
                        title: t[j],
                        start: d[j]
                    }
                ]

            }
        });
    });
</script>

I have used the fullcalendar 2.6.1. But nothing displayed. Please can anybody help me? I want to retrieve all the title and eventDate from the db and view in the calendar. The var t and d contains the all the data of $ta and $da arrays. I just wanted to assign them to events array title and start keywords.There are some red marks indicate that for loop is going to be wrong.

2 个答案:

答案 0 :(得分:1)

试试这个

<?php 
    $ta=array();
    $i=0;
?>
@foreach($events as $ev)
    <?php
        $ta[$i]['title'] = $ev->title;
        $ta[$i]['start'] = date('Y-m-d H:i:s', strtotime($ev->eventDate));
        $i++;
    ?>

@endforeach

<script>
    var t=<?php echo json_encode($ta)?>;

    $(document).ready(function() {


        $('#calendar').fullCalendar({

            //defaultDate: '2016-03-12',
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            events: t,
            eventRender: function(event, element) {
                $('.fc-time', element).hide();
            }
        });

    });

</script>

答案 1 :(得分:0)

    $.ajax({
              url:'',
              dataType: 'json',

              success: function(doc) {
                  var events = [];
              $.each(doc, function(key, value){
                          events.push({
                              title : value['title name'],
                              start : value['hours'],
                              backgroundColor: Metronic.getBrandColor('yellow'),

                              id : value['id']
                          });
                      });
                  AddFullCalenderEvent(events);
              }
          });

function AddFullCalenderEvent(eventList){
      $('#calendar').fullCalendar({
          events: eventList,

          eventClick: function(event) {
              // opens events in a popup window

              window.location.href = "pagename?id=" + event.id;

              return false;
          }

试试这个