Okay I have been racking my brain trying to build a JSON array from mysql. The array MUST be in the following format. I am using fullcalendar and want to make the events on the calendar dynamic. Below is the code that builds the array, but currently it does not get the information from 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/"
    )

));

推荐答案

Is something like this what you want to do?

$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);

It returns a json string in this format:

[{"id":"1","col1":"col1_value","col2":"col2_value"},{"id":"2","col1":"col1_value","col2":"col2_value"}]

OR something like this:

$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/"
),

     //2nd record
     array(
    'id' => 222,
    'title' => "Event2",
    'start' => "$year-$month-20",
    'end' => "$year-$month-22",
    'url' => "http://yahoo.com/"
)

);

echo json_encode($json_array);

Json相关问答推荐

Azure Devops Pipeline:SON字符串变量丢失所有双引号

无法根据vega规范中的条件设置文本 colored颜色

在Go中,当字段可以根据其他字段具有不同的类型时,什么是正确的方法来卸载JSON?

如何使用Aeson解码带有Unicode字符的JSON文件?

在AWS步骤函数中将字符串解析为JSON&S映射状态

基于 JSON 字段的 Jolt 条件标志

如何对未知/变量键的字典进行编码?

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

使用 TypeScript 接口时如何修复未定义错误?

如何在不使用 Newtonsoft.JSON 的情况下序列化/反序列化

从字节解码 JSON 数据,将 float 值更改为 int

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

Swift - 将图像从 URL 写入本地文件

如何使用 Swift 从 NSURLSession 获取 cookie?

JSON对象中的JavaScript递归搜索

如何在 Django JSONField 数据上聚合(最小/最大等)?

Sequelize - 如何仅返回数据库结果的 JSON 对象?

请求返回字节,我无法解码它们

case 类只有一个字段时如何将json转为 case 类

使用 JSONArray 和 JSONObject 进行 Foreach