从mysql填充嵌套的json

时间:2016-07-09 13:50:40

标签: php mysql json fullcalendar-scheduler

我正在尝试使用完整的日程安排程序插件,我正在尝试从我的数据库中创建所需的JSON。所需格式如下所示。我需要员工做他们部门的“孩子”

因此,所有清洁工都会在清洁,人力资源等人力资源下展示

   [
        {
            id: '1',
            title: 'Cleaning',
            children: [
                {
                    id: '2',
                    title: 'Cleaner 1'
                },
                {
                    id: '2',
                    title: 'Cleaner 2'
                }
            ]
        },
{
            id: '2',
            title: 'Human Resources',
            children: [
                {
                    id: '2',
                    title: 'Human resources 1'
                },
                {
                    id: '2',
                    title: 'Human resources 2'
                }
            ]
        }
    ]

我的表格如下所示:

Groups table:
ID | GROUP_NAME
1  | Human Resources
2  | Cleaning
3  | Warehouse

Staff table
ID | GROUP_ID | STAFF_NAME
1  | 1        | Human resources 1
2  | 1        | Human resources 2
3  | 2        | Cleaning 1
4  | 2        | Cleaning 2
5  | 3        | Warehouse 1

我的代码到目前为止,它只输出所有内容

$data = array();

$getgroups = mysqli_query($con, "SELECT groups.id, groups.group_name, person.name, person.id AS personid FROM groups INNER JOIN interns ON groups.id = interns.group_id");

while($group = mysqli_fetch_assoc($getgroups)) {

    $data[] = array(
        "id" => $group["id"], //Group ID
        "title" => $group["group_name"], //Group Name
        "children" => array([
            "id" => $group["id"],
            "title" => $group["personid"], //Intern name
            ])
    );
}

echo json_encode($data);

0 个答案:

没有答案