如何在代码点火器中显示从控制器到完全压延的数据

时间:2016-06-04 05:18:04

标签: php jquery ajax codeigniter fullcalendar

这是我的控制器

 function Test_Function()
    {


 // Event id;
    // event.title is the only place where you can "display" text;
    // event.start: What date should the event be placed
    $data = array
    (
        array
        (
            'id' => 1234, 
            'title' => "Mr Name xxxxx\nDate 2016-06-04T00:00:00\nBirthday\nemail sasa@asfda.com\nTelephone 1234567890",
            'start' => "2016-06-15"
        ),
    );
    // add more array(...events...) as required
    echo json_encode($data);    
 }

我需要使用ajax,如何显示结果

在完整日历中显示这些详细信息
   <script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        events: {
            url: '<?php echo site_url('Air/Get_calander_fare'); ?>',
            type: 'POST',
            data: {
                value1: 'aaa',
                value2: 'bbb'
            },
            success: function(data) {
                console.log(data);
            },
            error: function() {
                alert('there was an error while fetching events!');
            },     
        }
    });
});

我需要在$ data

的日期显示姓名,数据,电子邮件,电话

4 个答案:

答案 0 :(得分:0)

在PHP过程中,您必须回显字符串,使用json编码或任何其他字符串,但字符串。 AJAX期待字符串,你的字符串格式不正确。您可以手动输入example.tld/[index.php/]controller_name/test_function的URL进行检查(方括号部分以防您在apache中没有设置重写规则)并查看什么是echo或输出。如果您在Ubuntu中有解析错误,则默认情况下可以在/var/log/apache2/error.log中进行检查,但是如果需要,您可以检查它并在/etc/php5/apache2/php.ini中重新配置。位置取决于您的操作系统。您应该将$data字符串换成单引号而不是换成双引号。有效输出类似于:

$data = '{"Results":{"Results":[{"Title":"Mr","Name":"xxxxx","Date":"2016-06-04T00:00:00","Data":"Birthday","email":"sasa@asfda.com","Telephone":1234567890},{"Title":"Mr","Name":"yyyyy","Date":"2016-06-05T00:00:00","Data":"Birthday","email":"qwee@rrrrr.com","Telephone":7412589630},{"Title":"Mrs","Name":"zzzzzzz","Date":"2016-06-07T00:00:00","Data":"Anniversary","email":"asdaf@qwe.com","Telephone":1234567890}]}}';
echo $data;

答案 1 :(得分:0)

您有两种方法可以让您的数据显示在日历中,并且您的事件数据必须根据文档http://fullcalendar.io/docs/event_data/Event_Object/进行格式化。 http://fullcalendar.io/docs/event_data/events_function/http://fullcalendar.io/docs/event_data/events_json_feed/

中介绍了这两个选项

我要在php中格式化数据并将事件用作json,因为对我来说它更简单,但它需要更改Test_Function()。您可以通过以下方式获得Test_Function()回显数据:

function Test_Function()
{
    // Event id;
    // event.title is the only place where you can "display" text;
    // event.start: What date should the event be placed
    $data = array
    (
        array
        (
            'id' => 1234, 
            'title' => "Mr Name xxxxx\nDate 2016-06-04T00:00:00\nBirthday\nemail sasa@asfda.com\nTelephone 1234567890",
            'start' => "2016-06-06"
        ),
    );
    // add more array(...events...) as required
    echo json_encode($data);   
}

然后在jQuery中

作为JSON提要(旁注:你错过了封闭的事件):{...)

$('#calendar').fullCalendar({

    events: {
        url: '<?php echo site_url('Test/Test_Function'); ?>',
        type: 'POST',
        data: {
            value1: 'aaa',
            value2: 'bbb'
        },
        success: function(data) {
            console.log(data);
        },
        error: function() {
            alert('there was an error while fetching events!');
        },
    }
}); 

更短的版本

$('#calendar').fullCalendar({
    events: '<?php echo site_url('Test/Test_Function'); ?>'
});

选项:事件作为一种功能。 Test_Function()可以按原样使用,但字符串$data被双引号" "包围,因此您的$data变量不会被解释为字符串,而且很可能是php正在抛出另一个答案中提到的错误。要修改该更改,请将周围的双引号" "更改为' ',或者将转义序列\"添加到字符串中的所有双引号,以便将整个内容解释为字符串。修复$data后,您可以通过以下方式创建事件jQuery:

    $('#calendar').fullCalendar({

    events: function(start, end, timezone, callback) {
        $.ajax({
            url: '<?php echo site_url('Test/Test_Function'); ?>',
            type: 'POST',
            data: {
                value1: 'aaa',
                value2: 'bbb'
            },
            success: function(data) {
                var events = [];
               /* loop though all the events received from php and parse them and push them into an array
                var event = {
                    id = data[index]
                events.push(...);
            */
                callback(events);
            },
            error: function() {
                alert('there was an error while fetching events!');
            },
        });
    }
}); 

<强>更新
我正在使用FullCalendar v2.7.3和jQuery v2.1.4。下面是我用来获取事件的代码

php中的

Test_Function()

<?php
    Test_Function();
    function Test_Function()
    {
        // Event id;
        // event.title is the only place where you can "display" text;
        // event.start: What date should the event be placed
        $data = array
        (
            array
            (
                'id' => 1234, 
                'title' => "Mr Name xxxxx\nDate 2016-06-04T00:00:00\nBirthday\nemail sasa@asfda.com\nTelephone 1234567890",
                'start' => "2016-06-06"
            ),
        );
        // add more array(...events...) as required
        echo json_encode($data);        
    }
?>

在HTML中。根据您的设置更改事件网址。

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link href='../fullcalendar.css' rel='stylesheet' />
<link href='../fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='../lib/moment.min.js'></script>
<script src='../lib/jquery.min.js'></script>
<script src='../fullcalendar.min.js'></script>
<script>
    $(document).ready(function() {
        $('#calendar').fullCalendar({
            events: {
                url: './php/test-function.php',
                type: 'POST',
                data: {
                    value1: 'aaa',
                    value2: 'bbb'
                },
                success: function(data) {
                    console.log(data);
                },
                error: function() {
                    alert('there was an error while fetching events!');
                },     
            }
        });
    });
</script>
<style>

    body {
        margin: 40px 10px;
        padding: 0;
        font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
        font-size: 14px;
    }

    #calendar {
        max-width: 900px;
        margin: 0 auto;
    }

</style>
</head>
<body>

    <div id='calendar'></div>

</body>
</html>

答案 2 :(得分:0)

  

您可以在 eventRendar 函数中附加json数据。

(from p in parents from c in p.Childs select new { p.ParentName, c.ChildName })

答案 3 :(得分:-1)

而不是使用echo $data使用return $data来自动返回您想要的值。