使用Sql Server和PHP在jQuery Full日历中添加和加载事件的问题 [英] Events adding and loading problem in jQuery Full calendar using Sql Server and PHP

查看:74
本文介绍了使用Sql Server和PHP在jQuery Full日历中添加和加载事件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了jQuery Full Calendar.我的事件存储在MS SQL Server表(事件)中,我以"json"格式获取数据,但未呈现为Full Calendar,也未将事件添加到表中....请任何人能帮助我....

I used jQuery Full Calendar in my project.My events are stored in MS SQL Server table(events), I got the data in "json" format, but it is not render into Full calendar and events are not added into table.... Please can any body help me....

CREATE TABLE events (
  id  NUMERIC IDENTITY(1,1) NOT NULL,
  title varchar(255) NOT NULL,
  start_event datetime NOT NULL,
  end_event datetime NOT NULL,
  CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
  ( 
 id ASC 
 )
)

INSERT INTO events(title,start_event,end_event) VALUES
('meeting1', '2019-07-31 00:00:00', '2019-07-31 00:00:00'),
('meeting2', '2019-08-09 00:00:00', '2019-08-09 00:00:00');

application/views/index.php:

application/views/index.php:

$(document).ready(function() {
   var calendar = $('#calendar').fullCalendar({
    editable:true,
    header:{
     left:'prev,next today',
     center:'title',
     right:'month,agendaWeek,agendaDay'
    },
    events: {
      url : '<?php echo site_url('Home/load');?>',
      error: function() 
            {
                alert("error");
            },
      success: function()
            {
                console.log("successfully loaded");
            }
    },
    selectable:true,
    selectHelper:true,
    select: function(start, end, allDay)
    {
     var title = prompt("Enter Event Title");
     if(title)
     {
      var start = $.fullCalendar.formatDate(start,"Y-MM-DD HH:mm:ss");
      var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
      $.ajax({
       url:"<?php echo site_url('Home/valid_calendar');?>",
       type:"POST",
       data:{title:title, start:start, end:end},
       success:function()
       {
        calendar.fullCalendar('refetchEvents');
        alert("Added Successfully");
       }
      })
     }
    },

application/views/load.php:

application/views/load.php:

$timezone = new DateTimeZone("UTC");

$sql = "select * from events";

$stmt = sqlsrv_query($conn,$sql);

if($stmt == false)
{
    die(print_r(sqlsrv_errors(),true));
}

while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
      $data[] = array(

                  'title' => $row['title'],
                  'start_event' => $row['start_event']->format("Y-m-d H:i:s"),
                  'end_event' => $row['end_event']->format("Y-m-d H:i:s")
             );
}

echo json.parse($data);

我的错误:

未捕获的TypeError:无法读取未定义的属性"hasTime"

Uncaught TypeError: Cannot read property 'hasTime' of undefined

我认为PHP datetime和Sql Server datetime不匹配... 所以我在事件load.php中添加了以下代码

I thought PHP datetime and Sql Server datetime are mismatched... So I added following code in events load.php

$timezone = new DateTimeZone("UTC");

$data[] = array(

              'title' => $row['title'],
              'start_event' => $row['start_event']->format("Y-m-d H:i:s"),
              'end_event' => $row['end_event']->format("Y-m-d H:i:s")
         );



$(document).ready(function() {
   var calendar = $('#calendar').fullCalendar({
    editable:true,
    header:{
     left:'prev,next today',
     center:'title',
     right:'month,agendaWeek,agendaDay'
    },
    events:" <?php echo site_url('Home/load');?>",
    selectable:true,
    selectHelper:true,
    select: function(start, end, allDay)
    {
     var title = prompt("Enter Event Title");
     if(title)
     {
      var start = $.fullCalendar.formatDate(start,"Y-MM-DD HH:mm:ss");
      var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
      $.ajax({
       url:"<?php echo site_url('Home/valid_calendar');?>",
       type:"POST",
       data:{title:title, start:start, end:end},
       success:function()
       {
        calendar.fullCalendar('refetchEvents');
        alert("Added Successfully");
       }

它给我的输出为:

[
  {"title":"hi","start_event":"2019-07-31 00:00:00","end_event":"2019-07-31 00:00:00"},
  {"title":"dsfdfdfd","start_event":"2019-08-09 00:00:00","end_event":"2019-08-09 00:00:00"}
]

但是在我的网址中: http://localhost/calendar_sql/index.php/Home/load?start = 2019-06-30& end = 2019-08-11& _ = 1564490059946

在我的网址中,我没有将此数据传递到任何地方 start = 2019-06-30& end = 2019-08-11

In my url I don't passed this data anywhere start=2019-06-30&end=2019-08-11

推荐答案

您为事件提供的属性名称不正确.

The property names you're giving to your events are incorrect.

仔细研究事件对象文档.您已为事件指定了start_eventend_event属性,但是fullCalendar希望日期字段为startend.您不仅可以发明属性名称,还可以期望fullCalendar猜出您叫它们什么名称;它们必须与文档中提供的名称匹配,以便知道在哪里查找.

Study the event object documentation more closely. You have given your events start_event and end_event properties, but fullCalendar expects the date fields to be start and end. You can't just invent the property names and expect fullCalendar to guess what you called them; they must match the names given in the documentation so that it knows where to look.

错误消息是因为fullCalendar/momentJS无法找到已定义的start属性来检查它是否具有时间分量.

The error message is because fullCalendar/momentJS cannot find a defined start property to check whether it has a time component or not.

我希望将您的PHP更改为

I expect that changing your PHP to

$data[] = array(
  'title' => $row['title'],
  'start' => $row['start_event']->format("Y-m-d H:i:s"),
  'end' => $row['end_event']->format("Y-m-d H:i:s")
);

应该解决您的问题.

P.S.作为另一点,您提到了当调用/Home/load URL来获取事件时,URL最终以http://localhost/calendar_sql/index.php/Home/load?start=2019-06-30&end=2019-08-11&_=1564490059946的形式添加了start=2019-06-30&end=2019-08-11.这是预期的.根据将事件作为JSON feed文档,fullCalendar会自动将这些参数附加到每当它与您的服务器联系以获取事件数据时的URL.这个想法是您的服务器应该读取这些GET值,并使用它们来过滤仅返回那些在那些日期内发生的事件的列表.这样可以提高AJAX调用的效率,因此您不会在用户未查看的其他日期返回很多事件.

P.S. As a separate point, you mentioned that when you call to the /Home/load URL to get the events, the URL ends up as http://localhost/calendar_sql/index.php/Home/load?start=2019-06-30&end=2019-08-11&_=1564490059946 with start=2019-06-30&end=2019-08-11 added to it. This is expected. As per the events as a JSON feed documentation fullCalendar will automatically append these parameters to the URL whenever it contacts your server to fetch event data. The idea is that your server should read these GET values and use them to filter the list of events returned to only those which occur within those dates. This will make the AJAX call more efficient, so that you don't return lots of events for other dates which the user is not viewing.

P.P.S.

echo json.parse($data);

我认为这是您上面的代码中的错字?除非您编写或导入了一些自定义代码,否则PHP中没有像json.parse这样的功能(在这里解析没有意义,您将在编码(即创建)JSON,而不是解析(即读取)JSON.希望该行应显示为

I assume this is a typo in your code above? Unless you wrote or imported some custom code, then there is no such function as json.parse in PHP (and parsing would make no sense here, you would be encoding (i.e. creating) JSON, not parsing (i.e. reading) it. I would expect this line should read

echo json_encode($data);

这是您的真实代码吗?

这篇关于使用Sql Server和PHP在jQuery Full日历中添加和加载事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆