创建PHP json feed并成功将其链接到javascript
发布时间:2020-05-25 08:57:27 所属栏目:PHP 来源:互联网
导读:我有一个项目我正在努力,需要通过使用json feed到 javascript的php脚本从数据库发送信息.以下是脚本: 这是javascript: link rel= stylesheet type=text/css href=fullcalendar/fullcalendar/fullcalendar.css /link rel=stylesheet media=pr
|
我有一个项目我正在努力,需要通过使用json feed到 javascript的php脚本从数据库发送信息.以下是脚本: 这是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"}
感谢您的帮助. 实现所需结果的最佳方法是在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.
?>
我还简化并缩短了一些代码以删除不必要的双引号. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
