gpt4 book ai didi

php - 通过 php 处理 mysql 的 JSON 输出

转载 作者:行者123 更新时间:2023-11-29 00:47:57 25 4
gpt4 key购买 nike

好的,所以我设法弄清楚了 php 代码,当我在浏览器中查看它时,它正在工作并以正确的 json 格式输出。这是感谢 Starx 帮助的代码:

<?php
include ("Includes/dbConnect.php");

$_GET['date'];

$query2 = "SELECT * FROM events";
$checkevent = mysqli_query($cxn,$query2) or die("Couldn't execute query!");
$dates = array();
while ($row2 = mysqli_fetch_array($checkevent))
{
$eventDate = $row2['eventDate'];
$eventName = $row2['eventName'];
$eventHost = $row2['host'];

$dates[$eventDate] = array('title' => $eventName, 'desc' => $eventHost);

}
echo json_encode(array("dates" => $dates));
?>

这个输出: {"dates":{"2012-03-16":{"title":"表格测验","desc":"MSU"},"2012-03-20":{"title":"欢迎光临", "desc":"我"}}}

所以一定是我的jquery代码有问题。我根据 Starx 的规范对其进行了更改,但仍然没有,有人有任何想法吗??

    <script type="text/javascript">
$(document).ready(function()
{
$("#ical").ical({


beforeMonth:function(date)
{
$.ajax({
type: "GET",
url: "getCalendarEvents.php",
dataType: "json",
data: "date="+date,
async: false, //stop rendering the calender until eventdates is changed.
success: json.each(function(k,v){
$.fn.ical.changeEventDates(v); //this function changes the eventdates
}
})
}
});
});

</script>

最佳答案

确保回显编码后的json后没有进一步的操作。所以在最后使用 exit() 来确认这一点

echo json_encode($dates);
exit;

更新

在这种情况下,请注意您正在为 $date 使用二维数组。当使用 $date[] = array(..) 所以你必须像访问它一样

json.each(function(k,v) { 
//Now v will hold the json format
$.fn.ical.changeEventDates(v);
});

更新2

{"dates":{"eventDate":{"title": "eventName", "desc": "eventHost"}, "eventDate": {"title": "eventName", "desc": "eventHost"}}}

这种格式不太可能,因为每个项目都有相同的键 eventDate。你可以用另一种方式来做。在 php 中更改以下内容

echo json_encode(array("dates" => $dates));
exit;

现在,您的成功函数中的 json 将采用类似的格式

function(json){
// This will hold the json is this format
// {"dates":{ 0 :{"title": "eventName", "desc": "eventHost"}, 1 : {"title": "eventName", "desc": "eventHost"}}}
// Note the numeric indexing
}

关于php - 通过 php 处理 mysql 的 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com