创建PHP json feed并成功将其链接到javascript [英] Creating a PHP json feed and successfully linking it to javascript
问题描述
我有一个正在研究的项目,需要通过使用json feed到javascript的php脚本从数据库发送信息。以下是脚本:
I have a project i am working on that requires sending information from database through a php script using json feed to javascript. Here are the scripts:
这是javascript:
This is the 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脚本的样子:
This is how the php script should look like:
[ {"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"}
感谢您的帮助提前。
推荐答案
实现所需结果的最佳方法是在PHP中创建一个数组,然后使用 json_encode()
创建输出。你已经做了一些 - 你只需要多一点:
The best way to achieve the result you want is to create an array in PHP, then use json_encode()
to create the output. You're already doing some of this - you just need a little more:
<?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.
?>
我还简化并缩短了一些代码以删除不必要的双引号。
I've also simplified and shortened some of your code to remove the unnecessary double quotes.
这篇关于创建PHP json feed并成功将其链接到javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!