我需要从 PHP 到 jquery 获取一些 Json 数据。
我需要 javascript 方法中的以下格式。
function returnJson()
{
return {
events: [
{
"id": 1,
"start": new Date(2013, 4, 26, 12),
"end": new Date(2013, 4, 26, 13, 30),
"title": "Lunch with Mike"
},
{
"id": 2,
"start": new Date(2013, 4, 27, 14),
"end": new Date(2013,4, 27, 14, 45),
"title": "Dev Meeting"
}]
};
}
为此,我在 javascript 中执行以下操作:
function returnJson()
{
var eventResult = $.getJSON("../PHP/PhpAction.php?f=fetchCalendarEvent");
return eventResult;
}
在 php 中:
function fetchCalendarEvent()
{
$tablename = "tb_calendar";
$sql = "SELECT eventId,userId,enentName,eventText,EXTRACT(YEAR FROM startTime) AS startyear,EXTRACT(MONTH FROM startTime) AS startMonth,EXTRACT(DAY FROM startTime) AS startDay,EXTRACT(HOUR FROM startTime) AS startHour,EXTRACT(MINUTE FROM startTime) AS startMin,EXTRACT(YEAR FROM endTime) AS endyear,EXTRACT(MONTH FROM endTime) AS endMonth,EXTRACT(DAY FROM endTime) AS endDay,EXTRACT(HOUR FROM endTime) AS e`enter code here`ndHour,EXTRACT(MINUTE FROM endTime) AS endMin FROM ".$tablename." WHERE userId='".$_SESSION['userid']."' AND isActive=1";
$q = mysql_query($sql);
$i=1;
$eventData="{events: [";
if (!mysql_num_rows($q)) {
echo 'No records found';
}
else
{
while ($row = mysql_fetch_assoc($q)) {
$eventData.="{'id':".$row['eventId'].",";
$eventData.="'end': new Date(".$row['startyear'].",".$row['startMonth'].",".$row['startDay']."," .$row['startHour'].",".$row['startMin']."),";
$eventData.="'start': new Date(".$row['endyear'].",".$row['endMonth'].",".$row['endDay'].",".$row['endHour'].",".$row['endMin']."),";
$eventData.="'title':'".$row['enentName']."'},";
$i++;
}
}
$eventData= rtrim($eventData, ",");
$eventData.="]}";
echo json_decode($eventData);
}
我在 firebug 中检查 php 方法返回的数据如下:
{ events: [ {
'id': 2,
'end': new Date(2013, 4, 27, 18, 38),
'start': new Date(2013, 4, 27, 18, 38),
'title': 'test'
}, {
'id': 3,
'end': new Date(2013, 4, 23, 11, 0),
'start': new Date(2013, 4, 23, 14, 15),
'title': 'testing23'
}
] }
谁能帮帮我吗。我是php新手。任何帮助将不胜感激。