【问题标题】:Creating a PHP json feed and successfully linking it to javascript创建 PHP json 提要并成功将其链接到 javascript
【发布时间】:2013-11-02 11:17:12
【问题描述】:

我正在处理一个项目,该项目需要使用 json 提要通过 php 脚本将信息从数据库发送到 javascript。以下是脚本:

这是javascript:

<link rel= 'stylesheet' type='text/css' href='fullcalendar/fullcalendar/fullcalendar.css' />
<link rel="stylesheet" media="print" href="fullcalendar/fullcalendar/fullcalendar.print.css" />
<script type="text/javascript" src="fullcalendar/lib/jquery.min.js"></script>
<script type='text/javascript' src="fullcalendar/fullcalendar/fullcalendar.js"></script>
<script type="text/javascript" src="fullcalendar/lib/jquery-ui.custom.min.js" ></script>
<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        header: {

                left: 'prev,next today',

                center: 'title',

                right: 'month,basicWeek,basicDay'

            },
            editable: true,

            events: "public_calendar.php"
    })
});
</script>
</head>

<body>

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



 ?php require_once("includes/initialize.php"); ?>
<?php require_once(LIB_PATH.DS.'database.php'); ?>

<?php
    //Find all the events
    $events = Event::find_all();
         foreach($events as $event):

            $id = (int) $event->id;
            $title = "{$event->event_title}";
            $start = "{$event->start_date}" ." ". "{$event->start_time}";
            $end = "{$event->end_date}" ." ". "{$event->end_time}";
            $url = "event_detail.php";

            echo json_encode( array(
                'id'    => $id,
                'title' => "{$title}",
                'start' => "{$start}",
                'end'   => "{$end}",
                'url'   => "{$url}"
            ));         
        endforeach;

?>

这就是 php 脚本的样子:

[ {"id":111,"title":"Event1","start":"2013-10-10","url":"http:\/\/yahoo.com\/"},
  {"id":222,"title":"Event2","start":"2013-10-20","end":"2013-10-22","url":"http:\/\/yahoo.com\/"}
]

这就是现在的样子:

{"id":12,"title":"Matriculation","start":"2013-11-5 08:00","end":"2013-11-5 17:00","url":"event_detail.php"}
{"id":13,"title":"Exam","start":"2013-11-30 09:00","end":"2013-11-30 16:00","url":"event_detail.php"}
{"id":2,"title":"Convocation","start":"2013-12-11 08:00","end":"2013-12-11 19:00","url":"event_detail.php"}

提前感谢您的帮助。

【问题讨论】:

  • 不要将每个数组输出为 json String。将所有数组推入 main 数组并对其进行 json_encode。

标签: javascript php mysql json


【解决方案1】:

实现所需结果的最佳方法是在 PHP 中创建一个数组,然后使用json_encode() 创建输出。你已经在做这些了——你还需要更多:

<?php
//Find all the events
$events = Event::find_all();
$eventList = array();            // Assemble list of all events here

     foreach($events as $event):

       $eventList[] = array(              // Add our event as the next element in the event list
            'id'    => (int) $event->id,
            'title' => $event->event_title,
            'start' => $event->start_date." ".$event->start_time,
            'end'   => $event->end_date." ".$event->end_time,
            'url'   => "event_detail.php"
        );         
    endforeach;

    echo json_encode($eventList);       // encode and output the whole list.
?>

我还简化并缩短了您的一些代码以删除不必要的双引号。

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2012-11-16
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多