php - JIT 为组织结构图构建功能性 json

标签 php mysql json

我想构建 JIT SpaceTree 所需的 json 请求。

工作流程:问题以存储的回顾答案的 ID 开始。

  • 如果选项为 yes,则会根据 load_yes 值加载下一个问题。此 load_yes 值选择 ID 并加载该问题。

  • 如果选项为“否”,则会通过 load_no 下存储的 id 加载下一个无基础问题,

json 应该如下所示:

var json = {
        id: "start",
        name: "does test work?",
        data: {},
        children: [{
            id: "layer1_1",
            name: "option: no, id 3, Q: test does work with option no?",
            data: {},
            children: []
        }, {
            id: "layer1_2",
            name: "option: yes, id 2, Q:  test does work!!",
            data: {},
            children: [{
                id: "layer2_1",
                name: "option: no, id 4, Q: test does work?",
                data: {},
                children: []
            }, {
                id: "layer2_2",
                name: "option: yes, id 5, Q: ",
                data: {},
                children: []
            }]
        }]
    };
<小时/>
$query = $mysqli->query("SELECT * FROM topic_answer");

while($obj = $query->fetch_object()){
    $arr[] = array(
        'id' => $obj->id,
        'name' => $obj->topic_question,
        'data' => '',
        'children' => array(array(
            'id' => $obj->load_yes,
            'name' => $obj->load_yes,
            'data' => '',
            'children' =>array(),
            ),array(
                'id' => $obj->load_no,
                'name' => $obj->load_no,
                'data' => '',
                'children' => array(),

            ),
        )
    );
<小时/>
     id, topic_name,  topic_creator, topic_question,  load_yes,  start,  load_no,   end 
     1    test        jordan       does test work?         2          1       3          0
     4    test        jordan       test does work no       0          0       0          0
     5    test        jordan       test does work yes      0          0       0          0
     2    test        jordan       test does work yes!!    4          0       5          0

最佳答案

在 json 中,对象被描述为 {}。所以基本上这段代码的意思是一个对象数组:

[
  {},
  {}
]

所以在这里,你嵌套数组的地方:

'children' => array(array(
    'id' => $obj->load_yes,
    'name' => $obj->load_yes,
    'data' => '',
    'children' =>array(),
    ),array(
        'id' => $obj->load_no,
        'name' => $obj->load_no,
        'data' => '',
        'children' => array(),
    ),
)

你实际上想用一个对象替换内部数组,例如stdClass()。 stdClass 的使用方式如下:

$obj = new stdClass();
$obj->id = "layer1_1";
$obj->name = "option: no, id 3, Q: test does work with option no?";
$obj->data = new stdClass();
$obj->children = array();

然后,用php

'children' => array(
    $obj1,
    $obj2,
),

关于php - JIT 为组织结构图构建功能性 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394895/

相关文章:

MySQL 将值复制到另一个表

json - 如何从 OpenAPI/Swagger 模型定义生成 JSON 示例?

php - 使用 .htaccess 设置 upload_max_filesize 不起作用

php - ajax 调用 php 脚本不工作

php - 不带 GET 参数的请求字符串

MySQL空值不插入

mysql - 我如何将 "select"用于具有 "not in (a subquery)"的数据表

javascript - 在 Ajax 中解析 JSON

mysql node Select query with JSON information on WHERE statement

php - 在 Laravel 5.3 中强制换行