好的,我一直在绞尽脑汁尝试从mysql构建JSON数组。数组必须采用以下格式。我正在使用fullcalendar,并希望使日历上的事件动态化。下面是构建数组的代码,但当前它无法从mysql获取信息
$year = date('Y'); $month = date('m');
echo json_encode(array(
//Each array below must be pulled from database
//1st record
array(
'id' => 111,
'title' => "Event1",
'start' => "$year-$month-10",
'url' => "http://yahoo.com/"
),
//2nd record
array(
'id' => 222,
'title' => "Event2",
'start' => "$year-$month-20",
'end' => "$year-$month-22",
'url' => "http://yahoo.com/"
)
));
你想做这样的事情吗?
$return_arr = array();
$fetch = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) { $row_array['id'] = $row['id']; $row_array['col1'] = $row['col1']; $row_array['col2'] = $row['col2'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr); 它以以下格式返回json字符串:
[{"id":"1","col1":"col1_value","col2":"col2_value"},{"id":"2","col1":"col1_value","col2":"col2_value"}] 或类似这样的东西:
$year = date('Y'); $month = date('m');
$json_array = array(
//Each array below must be pulled from database //1st record array( 'id' => 111, 'title' => "Event1", 'start' => "$year-$month-10", 'url' => "http://yahoo.com/" ),来源:stack overflow
//2nd record
array(
'id' => 222,
'title' => "Event2",
'start' => "$year-$month-20",
'end' => "$year-$month-22",
'url' => "http://yahoo.com/"
)
);
echo json_encode($json_array);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。